7

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";

4
  • 3
    All 3 answers below, at this point in time are wrong, as they will not work if phani is the last element in the string. You are better off using Regex to match and replace. Commented Mar 6, 2013 at 10:43
  • 1
    Try to use Regex.Replace() Commented Mar 6, 2013 at 10:44
  • @bPratik Altough it is nice to be able to use Regex, I don't think it is really neccessary here. If this were to be a large string than it would be better to use a compiled Regex for performance, but I think a split and join is more readable here. Commented Mar 6, 2013 at 10:54
  • @Silvermind - yes, which is why you have my vote, but from a learners point of view, Regex is the solution to this category of issues and learning it is part of the trade :) Commented Mar 6, 2013 at 10:59

7 Answers 7

13
string str = "santhosh,phani,ravi,phani123,praveen,sathish,prakash"; var words = str.Split(','); str = String.Join(",", words.Where(word => word != "phani")); 
Sign up to request clarification or add additional context in comments.

1 Comment

and we need to just add the namespace using system.linq;
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 ^^)

Comments

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

Removes any given name from the list.

Comments

0

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()); 

1 Comment

I know - I just changed my answer.
0

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.

Comments

0
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 

2 Comments

What if "phani" is at the beginning or end of the string (i.e. not preceded or followed by the comma)?
Also \b doesn't make a sense much there, because comma itself is a word boundary character.
0

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 = @",?\bphani\b,?"; var regex = new Regex(pattern); var result = regex.Replace(input, ",").Trim(','); 

Shorter notation could look like this:

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

Explanation of the regular expression: ,?\bphani\b,? 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.