【发布时间】:2023-04-05 19:22:02
【问题描述】:
如何在字符串数组中搜索子字符串?我需要在字符串数组中搜索一个子字符串。字符串可以位于数组(元素)的任何部分或元素内。 (字符串中间)我尝试过:Array.IndexOf(arrayStrings,searchItem) 但 searchItem 必须是在 arrayStrings 中找到的完全匹配。在我的例子中,searchItem 是 arrayStrings 中完整元素的一部分。
string [] arrayStrings = {
"Welcome to SanJose",
"Welcome to San Fancisco","Welcome to New York",
"Welcome to Orlando", "Welcome to San Martin",
"This string has Welcome to San in the middle of it"
};
lineVar = "Welcome to San"
int index1 =
Array.IndexOf(arrayStrings, lineVar, 0, arrayStrings.Length);
// index1 mostly has a value of -1; string not found
我需要检查 arrayStrings 中是否存在 lineVar 变量。 lineVar 可以有不同的长度和值。
在数组字符串中找到该子字符串的最佳方法是什么?
【问题讨论】: