【问题标题】:Conversion of strtr php function in C#C#中strtr php函数的转换
【发布时间】:2016-02-02 03:38:32
【问题描述】:

需要在C#中转换这个php代码

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

是否存在等效的 C# 函数?

【问题讨论】:

    标签: c# php asp.net replace strtr


    【解决方案1】:

    @Damith @Rahul Nikate @Willem van Rumpt

    您的解决方案通常有效。有不同结果的特殊情况:

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

    返回

    ai hll, I shid aello
    

    而你的代码:

    ai all, I said aello
    

    我认为 php strtr 同时替换了输入数组中的字符,而您的解决方案执行替换,然后结果用于执行另一个替换。 所以我做了以下修改:

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

    所以代码

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

    报告与php相同的结果:

    ai hll, I shid aello
    

    【讨论】:

      【解决方案2】:

      PHP 方法 strtr() 是 translate 方法,而不是 string replace 方法。 如果您想在C# 中执行相同操作,请使用以下内容:

      根据你的 cmets

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

      注意:在C# 中没有与strtr() 完全相同的方法。

      You can find more about String.Replace method here

      【讨论】:

      • 请注意php manual中报告的示例echo strtr("baab", "ab", "01");返回1001使用您的代码,结果是ba01
      • echo strtr("hi all, I said hello","ah","ha"); 在 php 中有不同的结果。
      【解决方案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
      

      示例方法:

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

      【讨论】:

      • 请注意php manualecho strtr("baab", "ab", "01");中报告的示例返回1001使用您的代码,结果是ba01
      • 字符串输入="baab";字符串 f="ab";字符串 t="01"; for(int i=0; i
      • echo strtr("hi all, I said hello","ah","ha"); 在 php 中有不同的结果。
      【解决方案4】:

      PHP 的恐怖 奇迹...我被你的 cmets 弄糊涂了,所以在手册中查找。您的表单将替换单个字符(所有“b”都变为“1”,所有“a”变为“0”)。 C# 中没有直接的等价物,但只需替换两次即可完成工作:

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

      【讨论】:

      • echo strtr("hi all, I said hello","ah","ha"); 在 php 中有不同的结果。
      【解决方案5】:

      以防万一还有来自 PHP 的开发人员缺少 strtr php 函数。

      现在有一个字符串扩展: https://github.com/redflitzi/StrTr
      它具有用于字符替换的双字符串选项以及用于替换单词的 Array/List/Dictionary 支持。

      字符替换如下所示:

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

      单词替换:

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

      【讨论】:

        猜你喜欢
        • 2015-01-17
        • 2011-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-25
        • 1970-01-01
        • 2019-03-06
        • 1970-01-01
        相关资源
        最近更新 更多