Lewis Olive
Lewis Olive

Reputation: 13

C# - Replacing multiple text

I am having some trouble on replacing multiple text. I know to replace text is:

...Text.Replace("text", "replaced");

I did not have a clue on how to change multiple text and i tried the code below but it did not work and i made some search on the web to find help but i did not see nothing that could help me so i made this question. Here is what i have so far:

string[] List = 
{ 
    "1", "number1",
    "2", "number2",
    "3", "number3",
    "4", "number4",
};
writer.WriteLine(read.
    Replace(List[0], List[1]).
    Replace(List[2], List[3]).
    Replace(List[4], List[5])
    );
writer.Close();

Upvotes: 1

Views: 259

Answers (5)

Balázs
Balázs

Reputation: 2929

In your specific case, when you want to replace specifically numbers, you should not forget about regexes, with which you could do something like this:

Regex rgx = new Regex("\\d+");

String str = "Abc 1 xyz 120";

MatchCollection matches = rgx.Matches(str);

// Decreasing iteration makes sure that indices of matches we haven't
// yet examined won't change
for (Int32 i = matches.Count - 1; i >= 0; --i)
    str = str.Insert(matches[i].Index, "number ");

Console.WriteLine(str);

This way you replace any number (though that might be a weird need), but tweaking the regex to match your needs should solve your problem. You can also specify the regex to match certain numbers like this:

Regex rgx = new Regex("1|2|3|178");

It is a matter of taste but I think this is way cleaner than specifying a dictionary of find-replace pairs, though you can only use this method when you want to insert a prefix or something like that like you do in your example. If you have to replace a with b, c with d or whatnot - that is, you replace different items with different substitutions -, you will have to stick to the Dictionary<String,String> way.

Upvotes: 1

Martin Braun
Martin Braun

Reputation: 12579

I would use Linq to solve it:

StringBuilder read = new StringBuilder("1, 2, 3");

Dictionary<string, string> replaceWords = new Dictionary<string, string>();
replaceWords.Add("1", "number1");
replaceWords.Add("2", "number2");
replaceWords.Add("3", "number3");

replaceWords.ForEach(x => read.Replace(x.Key, x.Value));

Note: The StringBuilder is better here, because it would not store a new string in the memory on every replace operation.

Upvotes: 3

xXliolauXx
xXliolauXx

Reputation: 1313

If I understand right, you want to do multiple replacements without writing replace over ans over again.

I would suggest to write a method that takes a list of strings and an input string, and then loops through all elements and calls input.replace(replacorList[i]).

As far as I know, there is no premade implementation for multiple replacements in one method in .NET yet.

Upvotes: 2

DeshDeep Singh
DeshDeep Singh

Reputation: 1843

If you have any plans on having a dynamic number of replacements, which could change at any time, and you want to make it a bit cleaner, you could always do something like this:

// Define name/value pairs to be replaced.
var replacements = new Dictionary<string,string>();
replacements.Add("<find>", client.find);
replacements.Add("<replace>", event.replace.ToString());

// Replace
string s = "Dear <find>, your booking is confirmed for the <replace>";
foreach (var replacement in replacements)
{
   s = s.Replace(replacement.Key, replacement.Value);
}

Upvotes: 2

npinti
npinti

Reputation: 52185

What you could do would be to do something like so:

Dictionary<string, string> replaceWords = new Dictionary<string, string>();
replaceWords.Add("1", "number1");
...

StringBuilder sb = new StringBuilder(myString);

foreach(string key in replaceWords.Keys)
    sb.Replace(key, replaceWords[key]);

This way, you would only need to specify your keys in a collection. This would allow you to extract you replacement mechanism as a method which could, for instance, take in a dictionary of strings.

Upvotes: 7

Related Questions