简体   繁体   English

C#IndexOf,当单词是另一个单词的一部分时,该怎么做?

[英]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;

You may use below regex to find exact word in your string. 您可以使用以下正则表达式在字符串中查找确切的单词。

string text = "soak oak";
string searchText = "oak";
var index = Regex.Match(text, @"\b" + Regex.Escape(searchText) + @"\b").Index;

Output: 输出:

5

See the demo 观看演示

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
    }
  • \\s White space \\ s 空白

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在字符串 C# 中的另一个单词之后直接查找一个单词 - How to find a word directly after another word in a string C# 使用 c# 在 MS Word 中用另一个词替换一个词 - Replacing a word with another word in MS Word using c# C# 如何在单词段落范围内获取部分字线颜色 - C# How can i get part of the word line's color in a word paragraph range 如何在C#中逐字逐句迭代? - How to word by word iterate in string in C#? 如何在C#中将一个单词的所有实例替换为另一个 - How to replace all instance of one word with another in C# 如何使用正则表达式在C#中检查文本中是否存在特定单词或其他特定单词 - How to check in C# if a specific word or another specific word exists in a text using regular expression 如何使用c#将文本从一个Word文档复制到另一个Word文档 - How to copy Text from one Word Document to another Word Document using c# OpenXML SDK C#:如何将书签内容从 1 个 word 文件复制到另一个 word 文件 - OpenXML SDK C#: How to Copy Bookmark content from 1 word file to another word file 使用C#打开Word时如何禁用ms Word 2010中的另存为和保存按钮 - How to disable Save As and Save buttons from ms word 2010 when word open using c# 如果目标单词不是另一个单词的一部分,则使用正则表达式替换单词 - Word replacement using regex if the target word is not a part of another word
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM