C# preg_replace?

前端 未结 5 1544
猫巷女王i
猫巷女王i 2020-12-10 09:46

What is the PHP preg_replace in C#?

I have an array of string that I would like to replace by an other array of string. Here is an example in PHP. How can I do somet

5条回答
  •  没有蜡笔的小新
    2020-12-10 10:17

    public static class StringManipulation
    {
        public static string PregReplace(string input, string[] pattern, string[] replacements)
        {
            if (replacements.Length != pattern.Length)
                throw new ArgumentException("Replacement and Pattern Arrays must be balanced");
    
            for (int i = 0; i < pattern.Length; i++)
            {
                input = Regex.Replace(input, pattern[i], replacements[i]);                
            }
    
            return input;
        }
    }
    

    Here is what I will use. Some code of Jonathan Holland but not in C#3.5 but in C#2.0 :)

    Thx all.

提交回复
热议问题