user1464139
user1464139

Reputation:

Is there a way to do multiple replaces one after another in C#?

I am trying to duplicate javascript code that looks like this:

car = document.transcription.text1.value;
car = car.replace(/ん/g, "n");
car = car.replace(/つ/g, "tsu");
car = car.replace(/きゃ/g, "kya");
car = car.replace(/きゅ/g, "kyu");
car = car.replace(/きょ/g, "kyo");
...

There are at least another 50 replaces.

Is there a way for me to duplicate this functionality in C# by chaining one replace after another?

Upvotes: 2

Views: 88

Answers (4)

CodingYoshi
CodingYoshi

Reputation: 27009

Replace returns a string so keep calling Replace on the returned value like this:

var name = "How are you there buddy?";
name =
   name
      .Replace('H', '_')
      .Replace('o', '_')
      .Replace('w', '_');

Another Approach

NOTE: I think the first approach is simpler and more intuitive: Whether you use a dictionary or just a simple Replace, you still have to type up the replacing and replaced strings. You decide.

If you want to keep all the items to be replaced and the replacing items in a dictionary then you can use Linq's Zip method as well. Don't let the name throw you off because it has nothing to do with zipping as in zipping a file. It actually gets its name from how a zipper on clothes works. The zipper on clothes enumerates both sides of the zipper and either closes the teeth or opens the teeth one by one. That is exactly what Zip does also. You give it two IEnumerable<T>s and it will enumerate both of them and perform on operation during each enumeration. Here is how:

var name = "How are you there buddy?";
var replacer = new Dictionary<string, string>();
replacer.Add("H", "_");
replacer.Add("o", "-");
replacer.Add("e", "*");

var names = replacer.Zip(name, (x, y) => name = name.Replace(x.Key, x.Value))
   .Last(); 

This will be the output which is exactly what we expected:

_-w ar* y-u th*r* buddy?

Upvotes: 1

GGG
GGG

Reputation: 670

Yes, there is, you could use an extension method such as the following (if you need to use Regex such as for same expressions translating to the same letter sequence):

public static class RegexStringExtension
{
    public static String RegexReplace ( this String haystack, String regex, String replacement )
    {
        return new Regex ( regex ).Replace ( haystack, replacement );
    }
}

And then you could concat them like this (or just use normal Replace if they are all unique):

var car = // ... get the value somehow
car = car.RegexReplace ( "ん", "n" )
    .RegexReplace ( "つ", "tsu" )
    .RegexReplace ( "きゃ", "kya" )
    .RegexReplace ( "きゅ", "kyu" )
    .RegexReplace ( "きょ", "kyo" );
    // ... other replacements

I have tested the code and it seems to work: https://ideone.com/Yu7pUE

Upvotes: 0

Lali
Lali

Reputation: 2866

At least you need to tell the compiler what should be replaced with what?

Declare a dictionary and mention it as

Dictionary<string, string> replaces = new Dictionary<string, string>() 
{ 
    {"old1", "new1"},
    {"old2", "new2"},
    {"old3", "new3"}
};

Then in a loop, you can do this as

foreach (var item in replaces)
{
    str.Replace(item.Key, item.Value);
}

Upvotes: 2

Unlocked
Unlocked

Reputation: 648

As a simple answer,

car = car.replace(/ん/g, "n").replace(/つ/g, "tsu").replace(/きゃ/g, "kya").replace(/きゅ/g, "kyu").replace(/きょ/g, "kyo")...;

Although for 50+ replaces, you may want to just make a dictionary or array or whatever and loop through it.

Upvotes: 0

Related Questions