5

Need to convert this php code in C#

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

Does an equivalent C# function exist?

Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54
Roberto Mattea
  • 335
  • 2
  • 10

5 Answers5

4

@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
Roberto Mattea
  • 335
  • 2
  • 10
3
   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;
}
Damith
  • 62,401
  • 13
  • 102
  • 153
  • 1
    Please note the example reported in [php manual](http://php.net/manual/en/function.strtr.php) `echo strtr("baab", "ab", "01");` return `1001` Using your code the result is `ba01`. – Roberto Mattea Nov 02 '15 at 09:42
  • string input ="baab"; string f="ab"; string t="01"; for(int i=0; i< f.Length;i++) { input = input.Replace(f[i], t[i]); } – Damith Nov 02 '15 at 09:58
  • `echo strtr("hi all, I said hello","ah","ha");` has a different result in php. – Roberto Mattea Nov 02 '15 at 13:40
3

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

Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54
  • Please note the example reported in [php manual](http://php.net/manual/en/function.strtr.php) `echo strtr("baab", "ab", "01");` returns `1001` Using your code the result is `ba01`. – Roberto Mattea Nov 02 '15 at 09:45
  • `echo strtr("hi all, I said hello","ah","ha");` has a different result in php. – Roberto Mattea Nov 02 '15 at 13:40
1

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('/', '_')
Willem van Rumpt
  • 6,490
  • 2
  • 32
  • 44
1

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