[英]C# IndexOf, when word is part of another word, How to?
let's say I have string "soak oak"
and I want to have string index
of ( "oak"
), it returns me the index of where "oak"
starts in "soak"
( 1 ) but I want to find index of exact word "oak"
( 5 ), what do I need to do? 假设我有一个字符串 "soak oak"
,我想拥有一个字符串index
( "oak"
),它向我返回了"oak"
从"soak"
( 1 )开始的索引,但是我想找到确切单词的索引"oak"
( 5 ),我需要做什么?
string text = "soak oak";
char[] seperators = {' ', '.', ',', '!', '?', ':',
';', '(', ')', '\t', '\r', '\n', '"', '„', '“'};
string[] parts = text.Split(seperators,
StringSplitOptions.RemoveEmptyEntries);
text.IndexOf("oak"); // gets '1' because "oak" is in "soak"
// but I want to get 5 because of exact word "oak"
Regex approach 正则表达式方法
string text = "soak oak";
int result = Regex.Match(text, @"\boak\b").Index;
We can test indexes ( IndexOf
) in a loop : 我们可以在循环中测试索引( IndexOf
):
static HashSet<char> s_Separtors = new HashSet<char>() {
' ', '.', ',', '!', '?', ':', ';', '(', ')', '\t', '\r', '\n', '"', '„', '“'
};
private static int WordIndexOf(string source, string toFind) {
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(toFind))
return -1;
for (int index = source.IndexOf(toFind);
index >= 0;
index = source.IndexOf(toFind, index + 1)) {
if (index < 0)
return -1;
if ((index == 0 || s_Separtors.Contains(source[index - 1])) &&
(index >= source.Length - toFind.Length ||
s_Separtors.Contains(source[index + toFind.Length])))
return index;
}
return -1;
}
Demo: 演示:
// 5
Console.Write(WordIndexOf("soak oak", "oak"));
You can use regular expressions, you may also find it useful to use word boundaries defined by regular expressions: 您可以使用正则表达式,也可能会发现使用正则表达式定义的单词边界很有用:
string text = "soak oak";
var pattern = @"\boak\b";
var regex = new Regex(pattern);
foreach(Match m in regex.Matches(text)){
Console.WriteLine(m.Index);
Console.WriteLine(m.Value);
}
You could find the string in your array by converting it to a list and using the IndexOf() method. 您可以通过将其转换为列表并使用IndexOf()方法在数组中找到该字符串。
parts.ToList().IndexOf("oak");
That tells you which array item it is, rather than the index in the original string. 那会告诉您它是哪个数组项,而不是原始字符串中的索引。
Another RegEx approach- 另一种RegEx方法-
string text = "soak oak";
var match = Regex.Match(text, @"\s[oak]");
if (match.Success)
{
Console.WriteLine(match.Index); // 4
}
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.