How to use regexp for finding patterns in strings?

后端 未结 2 731
眼角桃花
眼角桃花 2021-01-25 21:53

I have many patterns that want to find matched string in many string arrays and replace them with \"NON\" string. for example, if we have:

str[0]={\"this\",\"is\         


        
相关标签:
2条回答
  • 2021-01-25 22:21

    If you want a mutating solution:

    public void ReplaceStrs(string[] srcStrs, string[] patterns, string changeTo)
    {
        for (int i=0; i < srcStrs; i++) {
            if (Array.IndexOf(patterns, toChange[i]) >= 0) {
                srcStrs[i] = changeTo;
            }
        }
    }
    

    Non-mutating:

    public string[] ReplaceStrs(string[] srcStrs, string[] patterns, string changeTo) {
         srcStrs.Select(s => Array.IndexOf(patterns, s) >= 0 ? changeTo : s).ToArray();
    }
    
    0 讨论(0)
  • 2021-01-25 22:21

    A one liner just for fun :]

    str = Array.ConvertAll(str, a => Array.ConvertAll(a, i => patterns.Contains(i) ? "NON" : i));
    
    0 讨论(0)
提交回复
热议问题