2

I am trying to replace a specific sub string from a string in C#.

My string is: This is a car.

And I am trying to replace 'a' with string.Empty from the string with this code:

data = data.Replace("a", string.Empty);

But my output is :

This is c r.

I just want to remove isolated occurence of 'a', and not when this char/word is used in some other word (like car).

I want a output like ths: This is car.

How can I do this in C#?

1
  • Wow, many people who are hot on the triggers :-) Commented Jan 16, 2013 at 21:27

7 Answers 7

5

You need a regex pattern that only matches "a" on word boundaries. "\b" in a regex pattern denotes a word boundary:

Regex.Replace("this is a car", @"\ba\b", "")

If you want to match uppercase "A" as well, make sure your pattern is ignoring case (RegexOptions.IgnoreCase) or explicitly add "A" to the pattern like "\b[Aa]\b".

Sign up to request clarification or add additional context in comments.

2 Comments

This will leave two adjacent spaces in the given test case.
@WizardofOgz It behaves similar to the code written. Of course, that line does not match the output, so I am not sure what exactly is the desired behavior.
2

What do you mean with 'isolated occurence'? Is it perhaps something like this you're really after:

data.Replace(" a ", " ");

Comments

2

You need to do couple of different cases but it is certainly possible:

  • a in the beginning of the sentence "A car went by" -> s.StartsWith("A ")

  • a in the middle "this is a car" -> Replace(" a ", " ")

3 Comments

You forgot the case where the string ends with 'a', required since the string to replace has a trailing whitespace :-)
This is acceptable solution, but does not seems to be an elegant way of doing it. Is there better way to achieve this? Thanks for your answer!
@Arry - What don't you like about Mehrdad Afshari's solution?
1

search for the spaces too.

data = data.(" a ", " ")

3 Comments

someone disagrees with this answer apparently
Yeah, it's kind of funny to see four people shoot the same one-liner within a couple of seconds, but all of them down-voted at the same time is even more hilarious
See @Zdravko Danev for the cases your solution does not cover.
1

You can use a regex and limit to instances of 'a' that qualify as a word.

string input = "This is a car. A thing to watch out for.";
string output = Regex.Replace(input, "\ba\b", "");

//Results in "This is car. A thing to watch out for."

You can also add a character class to deal with capital and lower case, as well as any other characters. ex. \s[Aa]\s

The \s means any white space character

1 Comment

Can you please explain this with an example?
0

Search for the string " a " instead, and replace with a space to keep the sentence construct correct;

data = data.Replace(" a ", " ");

Comments

0

This seems like a great time to use the replace Method of the StringBuilder class.

StringBuilder.Replace Method

Replaces all occurrences of a specified character or string in this instance with another specified character or string.

OVERLOADS Replace(Char, Char) Replaces all occurrences of a specified character in this instance with another specified character.

Replace(String, String) Replaces all occurrences of a specified string in this instance with another specified string.

Replace(Char, Char, Int32, Int32) Replaces, within a substring of this instance, all occurrences of a specified character with another specified character.

Replace(String, String, Int32, Int32) Replaces, within a substring of this instance, all occurrences of a specified string with another specified string.

string myString = "This is a car.";
StringBuilder sb = new StringBuilder(myString);
sb.Replace(' a ', ' ');

SOURCE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.