简体   繁体   English

C#中strtr php函数的转换

[英]Conversion of strtr php function in C#

Need to convert this php code in C#需要在 C# 中转换此 php 代码

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

Does an equivalent C# function exist?是否存在等效的 C# 函数?

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

The PHP method strtr() is translate method and not string replace method. PHP方法strtr()是translate方法而不是string replace方法。 If you want to do the same in C# then use following: 如果你想在C#做同样的事情,那么使用以下内容:

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# . 注意:在C#没有类似strtr()完全相似的方法。

You can find more about String.Replace method here 您可以在此处找到有关String.Replace方法的更多信息

The horrors wonders of PHP...I got confused by your comments, so looked it up in the manual. PHP的恐怖奇迹...我对你的评论感到困惑,所以在手册中查了一下。 Your form replaces individual characters (all "b"'s get to be "1", all "a"'s get to be "0"). 你的表单取代了单个字符(所有“b”都变为“1”,所有“a”变为“0”)。 There's no direct equivalent in C#, but simply replacing twice will get the job done: 在C#中没有直接的等价物,但只需更换两次就可以完成工作:

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

@Damith @Rahul Nikate @Willem van Rumpt @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. 我认为php strtr替换输入数组中的字符,而你的解决方案执行替换然后结果用于执行另一个。 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: 报告与php相同的结果:

ai hll, I shid aello

Just in case there are still developers that come from PHP and are missing the strtr php function.以防万一仍有来自 PHP 的开发人员缺少 strtr php 函数。

There is now a String extension for this: https://github.com/redflitzi/StrTr现在有一个字符串扩展: 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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM