Arry
Arry

Reputation: 1690

Replace specific string C#?

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#?

Upvotes: 2

Views: 853

Answers (7)

Neil Meyer
Neil Meyer

Reputation: 585

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

Upvotes: 0

colin-higgins
colin-higgins

Reputation: 1117

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

Upvotes: 1

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422290

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

Upvotes: 5

Z .
Z .

Reputation: 12837

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 ", " ")

Upvotes: 2

Christoffer
Christoffer

Reputation: 12918

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

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

Upvotes: 2

search for the spaces too.

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

Upvotes: 1

RJ Lohan
RJ Lohan

Reputation: 6527

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

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

Upvotes: 0

Related Questions