1

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();
1
  • 1
    If you have a string like this "this is a string" and your replacing list is { "this", "string", "string", "something" }, what is the expected output? "string is a something" or "something is a something"? Commented Jul 20, 2015 at 7:22

5 Answers 5

7

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.

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

5 Comments

it only replaces 1 to number 1 but when i add replaceWords.Add("2", "number2"); it does not replace and messes up.
@LewisOlive: Can you please show the code you are using?
i just edited it and updated the code with you're code and i also put the output. thank you and all the rest of the people that helped me.
i am sorry, it is working nice! somehow it was not working but know it is, i am sorry and thank you for the help!
@LewisOlive Yes, you forgot to assign the text variable again. Rather use StringBuilder for multiple replace operations, as it will not restore the entire string in your memory every time you do a replacement.
3

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.

Comments

2

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

Comments

2

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.

Comments

1

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.

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.