How to use regexp for finding patterns in strings?

后端 未结 2 733
眼角桃花
眼角桃花 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();
    }
    

提交回复
热议问题