Finding All Substring Positions in a Larger String in C#
When working with large strings, locating all instances of a specific substring can be crucial for parsing and data analysis. This article addresses the challenge of finding all such instances and storing their indexes in a list.
Problem Statement
You have a large string that you need to analyze. Within this string, you need to identify and store the positions of every occurrence of a specific substring. Let's assume the example string is "extract"(me,i-have lots. of]punctuation." You wish to find all occurrences of this substring within the larger string and add their indexes to a list.
Resolution
Method 1:
The IndexOf method in C# can be used to locate the first occurrence of a substring. However, to find all occurrences, we need to use a loop to iterate through the string and continue searching until no more instances are found.
This technique can be improved by using an extension method to make the code more concise. Here's an example extension method:
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); } }
Method 2:
Alternatively, an iterator can also be used to yield the indexes sequentially:
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; } }
Both these methods can be easily added to any string object, making it convenient to find all substring positions. For instance, the following line would find all occurrences of substring "foo" within the string "fooStringfooBar":
List<int> indexes = "fooStringfooBar".AllIndexesOf("foo");
The above is the detailed content of How Can I Find All Occurrences of a Substring within a Larger String in C#?. For more information, please follow other related articles on the PHP Chinese website!