Roberto Mattea
Roberto Mattea

Reputation: 335

Conversion of strtr php function in C#

Need to convert this php code in C#

strtr($input, '+/', '-_')

Does an equivalent C# function exist?

Upvotes: 5

Views: 1729

Answers (5)

AggiWelters
AggiWelters

Reputation: 21

Just in case there are still developers that come from PHP and are missing the strtr php function.

There is now a String extension for this: https://github.com/redflitzi/StrTr
It has the two-strings option for character replacement as well as Array/List/Dictionary support for replacing words.

Character replacement looks like this:

var output = input.StrTr("+/", "-_");

Words replacement:

var output = input.StrTr(("hello","hi"), ("hi","hello"));

Upvotes: 1

Roberto Mattea
Roberto Mattea

Reputation: 335

@Damith @Rahul Nikate @Willem van Rumpt

Your solutions generally work. There are particular cases with different result:

echo strtr("hi all, I said hello","ah","ha");

returns

ai hll, I shid aello

while your code:

ai all, I said aello

I think that the php strtr replaces the chars in the input array at the same time, while your solutions perform a replacement then the result is used to perform another one. So i made the following modifications:

   private string MyStrTr(string source, string frm, string to)
    {
        char[] input = source.ToCharArray();
        bool[] replaced = new bool[input.Length];

       for (int j = 0; j < input.Length; j++)
            replaced[j] = false;

        for (int i = 0; i < frm.Length; i++)
        {
            for(int j = 0; j<input.Length;j++)
                if (replaced[j] == false && input[j]==frm[i])
                {
                    input[j] = to[i];
                    replaced[j] = true;
                }
        }
        return new string(input);
    }

So the code

MyStrTr("hi all, I said hello", "ah", "ha");

reports the same result as php:

ai hll, I shid aello

Upvotes: 4

Rahul Nikate
Rahul Nikate

Reputation: 6337

The PHP method strtr() is translate method and not string replace method. If you want to do the same in C# then use following:

As per your comments

string input = "baab";
var output = input.Replace("a", "0").Replace("b","1");

Note : There is no exactly similar method like strtr() in C#.

You can find more about String.Replace method here

Upvotes: 3

Damith
Damith

Reputation: 63065

   string input ="baab";
   string strfrom="ab";
   string strTo="01";
   for(int i=0; i< strfrom.Length;i++)
   {
     input = input.Replace(strfrom[i], strTo[i]);
   }
   //you get 1001

sample method:

string StringTranslate(string input, string frm, string to)
{
      for(int i=0; i< frm.Length;i++)
       {
         input = input.Replace(frm[i], to[i]);
       }
      return input;
}

Upvotes: 3

Willem van Rumpt
Willem van Rumpt

Reputation: 6570

The horrors wonders of PHP...I got confused by your comments, so looked it up in the manual. Your form replaces individual characters (all "b"'s get to be "1", all "a"'s get to be "0"). There's no direct equivalent in C#, but simply replacing twice will get the job done:

string result = input.Replace('+', '-').Replace('/', '_')

Upvotes: 1

Related Questions