简体   繁体   中英

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?

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 :

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.

parts.ToList().IndexOf("oak");

That tells you which array item it is, rather than the index in the original string.

Another RegEx approach-

    string text = "soak oak";
    var match = Regex.Match(text, @"\s[oak]");
    if (match.Success)
    {
        Console.WriteLine(match.Index); // 4
    }
  • \\s White space

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM