Home > Backend Development > C++ > How Can I Efficiently Find All Occurrences of a Substring in a C# String?

How Can I Efficiently Find All Occurrences of a Substring in a C# String?

Susan Sarandon
Release: 2025-01-01 12:36:10
Original
959 people have browsed it

How Can I Efficiently Find All Occurrences of a Substring in a C# String?

Finding All Positions of a Substring in a Larger String in C#

Finding the occurrences of a substring within a larger string is a common programming task. In C#, the string.IndexOf() method provides a convenient way to find the first occurrence of a substring, but it does not provide a straightforward way to find all occurrences.

To find all occurrences of a substring, you can use a loop that iterates through the larger string while using the string.IndexOf() method to locate each occurrence. However, this approach can be inefficient if the larger string is large and the substring is found multiple times.

A more efficient approach is to use an extension method, which allows you to add custom methods to existing classes. Here's an example of an extension method that finds all occurrences of a substring in a string:

public static List<int> AllIndexesOf(this string str, string value)
{
  if (String.IsNullOrEmpty(value))
    throw new ArgumentException("the string to find may not be empty", "value");
  List<int> indexes = new List<int>();
  for (int index = 0;; index += value.Length)
  {
    index = str.IndexOf(value, index);
    if (index == -1)
      return indexes;
    indexes.Add(index);
  }
}
Copy after login

With this extension method, you can find all occurrences of a substring in a string using the following syntax:

List<int> indexes = "fooStringfooBar".AllIndexesOf("foo");
Copy after login

Alternatively, you can also use an iterator to find all occurrences of a substring:

public static IEnumerable<int> AllIndexesOf(this string str, string value)
{
  if (String.IsNullOrEmpty(value))
    throw new ArgumentException("the string to find may not be empty", "value");
  for (int index = 0;; index += value.Length)
  {
    index = str.IndexOf(value, index);
    if (index == -1)
      break;
    yield return index;
  }
}
Copy after login

This iterator allows you to iterate through the occurrences of the substring using the foreach statement:

foreach (int index in "fooStringfooBar".AllIndexesOf("foo"))
{
  // do something with the index
}
Copy after login

The above is the detailed content of How Can I Efficiently Find All Occurrences of a Substring in a C# String?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template