我有很多字符串被分割成许多标记(例如inArray)。我需要找到所有特殊的子串(例如模式),并用“NN”替换它们。每个子字符串/模式都连接了令牌,但它们在每个字符串数组中可能是单独的标记(例如inArray),顺序很重要。 例如,如果我们有:
string[] inArray0={"this", "is", "simple", "text", "for", "example", "!" };
string[] inArray1= {"Can","you","help me","please","?","thank","you","very much","." };
string[]inArray2={"How","much","is","for","example","for","testing","your","solution","."};
string[] patterns = { "the", "that", "(for width)" ,"123", ".", "text", "for example", "help me","very much"};
期望的输出是:
inArray0={ "this", "is", "simple", "NN", "NN", "NN", "!" };
inArray1= { "Can", "you", "NN", "please", "?", "thank", "you", "NN", "NN" };
inArray2={"How","much","is", "NN", "NN", "for","testing","your","solution","NN"};
我使用了Follwoing方法,但无法正常工作。
changArray(string[] inArray,string[] patterns)
{
List<string> tmp = new List<string>();
foreach (string pattern in patterns)
{
tmp.Add(pattern);tmp.AddRange(pattern.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));}
var allPatterns = tmp.Distinct().ToList();
{
if (allPatterns.Contains(inArray[i]))
inArray[i] = "NN";
}
如何更改它还是有其他解决方案?
答案 0 :(得分:0)
这样可行,但您需要为每个数组执行此操作。
foreach (string s in array0.Where(w => patterns.Contains(w)))
{
S = "NN";
}
答案 1 :(得分:0)
我将@ JustSomeDude的代码包装到一个可以为每个输入数组调用的方法中。
internal void YourDesiredMethodName (string[] input)
{
foreach (string s in input.Where(w => patterns.Contains(w)))
{
s="";
for(i=0;i<s.Split(' ').Count();i++)
{
if(i+1==s.Split(' ').Count())
s += "NN";
else
s += "NN ";
}
}
}
用法:
YourDesiredMethodName(inArray0);
YourDesiredMethodName(inArray1);
YourDesiredMethodName(inArray2);