如何在许多字符串中找到模式并替换它们? [英] How to find patterns in many strings and replace them ?

查看:70
本文介绍了如何在许多字符串中找到模式并替换它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多模式想要在许多输入字符串数组中找到具有这些模式的匹配字符串,并用NON字符串替换它们。例如,如果我们有:

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

str[0]={"this","is","a simple","text","for","example","!"};
str[1]={"Can","you","help me","please","?","thank","you","very much","."};
string[] patterns={"the","that","(eg width)","123",".","text","for example","help me","very much"};



将str [i]中的匹配模式替换为NON是期望的结果:




Replace matched patterns in str[i] with "NON" is the desire result:

str[0]={"this","is","a simple","NON","NON","NON","!"};
str[1]={"Can","you","NON","please","?","thank","you","NON"};



我怎么能这样做吗?



我尝试了什么:



查找模式许多字符串数组并替换它们。


How to I can do it?

What I have tried:

finding patterns in many string arrayes and replace them.

推荐答案

试试这个



try this

static void Main(string[] args)
   {


       string[] myArray = { "Can", "you", "help me", "please", "?", "thank", "you", "very much", "." };
       ReplaceWithNON(myArray);


   }

   private static void ReplaceWithNON(string[] myArray)
   {
       string[] patterns = { "the", "that", "(eg width)", "123", ".", "text", "for example", "help me", "very much" };

       for (int i = 0; i < myArray.Length; i++)
       {
           if (patterns.Contains(myArray[i]))
               myArray[i] = "NON";
       }
   }


这篇关于如何在许多字符串中找到模式并替换它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆