How to remove the exact occurence of characters from a string?

For Example, I have a string like :

string str = "santhosh,phani,ravi,phani123,praveen,sathish,prakash";

I want to delete the charaters ,phani from str.

Now, I am using str = str.Replace(",phani", string.Empty);

then my output is : str="santhosh,ravi123,praveen,sathish,prakash";

But I want a output like : str="santhosh,ravi,phani123,praveen,sathish,prakash";

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

string str = "santhosh,phani,ravi,phani123,praveen,sathish,prakash";
var words = str.Split(',');
str = String.Join(",", words.Where(word => word != "phani"));

Method 2

the better choice is to use a Split and Join method.
Easy in Linq :

String str = "santhosh,phani,ravi,phani123,praveen,sathish,prakash";
String token = "phani";
String result = String.Join(",", str.Split(',').Where(s => s != token));

(edit : I take time for testing and i’m not first ^^)

Method 3

String.join(",", str.split(',').ToList().remove("phani"));

Removes any given name from the list.

Method 4

How about

str = str.Replace(",phani,", ",");

This, however, does not work if “phani” is the last item in the string. To get around this, you could do this:

string source = "...";
source += ",";    // Explicitly add a comma to the end

source = source.Replace(",phani,", ",").TrimEnd(',');

This adds a comma, replaces “phani” and removes the trailing comma.

A third solution would be this:

str = String.Join(",", str.Split(',').ToList().Remove("phani").ToArray());

Method 5

Try to use with comma instead of;

string str = "santhosh,ravi,phani,phani123,praveen,sathish,prakash";
str = str.Replace(",phani,", ",");
Console.WriteLine(str);

Output will be;

santhosh,ravi,phani123,praveen,sathish,prakash

Here is a DEMO.

As Davin mentioned in comment, this won’t work if phani is last item in the string. Silvermind’s answer looks like the right answer.

Method 6

string str = "santhosh,phani,ravi,phani123,praveen,sathish,prakash";
string pattern = @"b,phani,b";
string replace = ",";
Console.WriteLine(Regex.Replace(str, pattern, replace));

Output:

santhosh,ravi,phani123,praveen,sathish,prakash

Method 7

You may use the regular expression, but you have to take care of cases when your string starts or ends with the substring:

var pattern = @",?bphanib,?";
var regex = new Regex(pattern);
var result = regex.Replace(input, ",").Trim(',');

Shorter notation could look like this:

var result = Regex.Replace(input, @",?bphanib,?", ",").Trim(',');

Explanation of the regular expression: ,?bphanib,? matches the word phani, but only if preceded and followed by word-delimiter characters (because of the word boundary metacharacter b), and it can be (but doesn’t have to be) preceded and followed by the comma thanks to ,? which means none or more comma(s).

At the end we need to remove possible commas from the beginning and end of the string, that’s why there’s Trim(',') on the result.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x