Suppose you have a string ("haystack"), and a substring you want to search for ("needle"):
string haystack = "D0B11234 AM21ANP AM23AN1";
string needle = "AM21AN";
I'm looking for a way to find the index or substring after the substring being sought.
Calling IndexOf()
returns the start position of the needle, such that index = 9
in this case:
int index = haystack.IndexOf(needle);
My initial thought was to use LastIndexOf()
, but that returns the start index of the last occurrence of the needle in the haystack.
As is answered in this similar question (and probably others), if you wanted to locate the character immediately following needle
, it is required to increment index
by the length of the needle like this:
int index = haystack.IndexOf(needle);
int indexAfterNeedle = index + needle.Length;
Is there a method (existing, or perhaps easy to extend) aside from typing index + needle.Length
every time that allows you to determine the ending index of a substring?
The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.
Python String find() method returns the lowest index or first occurrence of the substring if it is found in a given string. If it is not found, then it returns -1.
IndexOf(String substring, int startindex)This Java substring indexOf() method returns the index of the first character in the substring passed as the first parameter, after the “startindex” index value. If substring starts from the passed integer value of “startindex”, that substring would be ignored.
No, there's nothing built into the BCL.
It's trivial to add yourself through an extension method:
public static int EndIndexOf(this string source, string value)
{
int index = source.IndexOf(value);
if (index >= 0)
{
index += value.Length;
}
return index;
}
Demo:
string testString = "D0B11234 AM21ANP AM23AN1";
string stringToSearch = "AM21AN";
int endIndex = testString.EndIndexOf(stringToSearch);
Console.WriteLine(testString);
Console.WriteLine(endIndex);
Console.WriteLine(testString.Substring(endIndex));
Output:
D0B11234 AM21ANP AM23AN1
15
P AM23AN1
This may seem like an off-by-one error depending on your requirements, so you may want to change it to index += value.Length - 1
to return the position of the last character of value
, instead of the index of the character after that.
You also may want to add an overload with an int startIndex
parameter, and actually call that one from the method above with a startIndex: 0
.
And be sure to add error handling to the extension method, like throwing an ArgumentNullException
and such.
Also, if the string to be found lies at the end of the source string, you cannot use the value returned by this method to take a substring of the source string, as it'll lie outside the source string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With