简体   繁体   中英

How to find the index of first occurrence of a specific type of character after some substring?

I have a string from in which I want to find where a specific substring occurrs:

int startIndex = str.IndexOf(substr);
int endIndex = str.IndexOf(" ", startIndex);

In the above example, I have found the endIndex by looking for a space that comes after startIndex . This is wrong and only just an example. I have a requirement to stop the search that as soon as any alphanumeric or special characters comes except space .

I know with the help of Regex we can do it but not getting how to combine it with IndexOf here in my this code. How can it be done, or how to find the required endIndex ?

Regex.Match has a positional parameter. You could use it to search from a starting point in the string.

Here's an example. Notice the regex matches any word character. That's assuming there was a typo in your post and you actually want to delimit on encountering any non -alphanumeric or special characters.

string s = "This is an example, and it contains a comma.";
int startIndex = s.IndexOf("example");
Regex r = new Regex(@"[\w]+");
Match m = r.Match(s, startIndex);
int endIndex = m.Success ? m.Index + m.Length : -1;

If you actually do want to delimit as soon as you encounter alphanumeric or special characters, change the regex's pattern to [\\s]+ .

Below is one solution without boundary checkings.

var charIndexAfterSubString = mainString.IndexOf(charToFind, 
                               mainString.IndexOf(substring) + substring.Length);

First of all, if you want to find the endIndex after the substring occurrence, then your current code has one more flaw:

int startIndex = str.IndexOf(substr);
int endIndex = str.IndexOf(" ", startIndex);

you are searching for the endIndex right from the startIndex . Suppose your STR and SUBSTR are:

pos: 0123456789012345678901234567890123456789012
str: The quick brown fox jumps over the lazy dog
sub: fox jumping
                     ^  !

Here, indexOf(sub) returns 16 (the ^ ), and if you look for a space right from 16, you will hit the space in between fox and jumping (the ! ).

To start looking after the substring, you must .. start looking after the substring , not at the substring .

int startIndex = str.IndexOf(substr);
int endIndex = str.IndexOf(" ", startIndex + substr.Length);

That's a first correction you need, if you'd like to keep your code.

The second thing is to look not for the space, but for the actual delimiters you want. In .Net String class you have not only the IndexOf method that takes one character to look for, you also have IndexOfAny method that can look for a set of characters and returns the position of the first match. For example:

var chars = new [] { 'r', 'o', 'v' };

int startIndex = str.IndexOf(substr);
int endIndex = str.IndexOfAny(chars, startIndex + substr.Length);

pos: 0123456789012345678901234567890123456789012
str: The quick brown fox jumps over the lazy dog
sub: fox jumping
                     ^        ?!

That will start looking at the space after fox jumps (since I added substr.length as before) and will look for any of 'r' 'o' and 'v'. So, it will hit the 'o' in 'over'.

You can adjust the chars array to any delimiters you would like to find.

For looking for a character from a certain set, you can also use Regex class. This example does exactly the same thing as IndexOfAny above:

var regex = new Regex("[rov]");

int startIndex = str.IndexOf(substr);
var match = regex.Match(str, startIndex + substr.Length);

int endIndex = match.Index;

pos: 0123456789012345678901234567890123456789012
str: The quick brown fox jumps over the lazy dog
sub: fox jumping
                     ^        ?!

The regex will start looking right at the space after the fox jumps (as before), and will look for matches for [rov] expression (which means: any character like ROV). So, the effect is the same.

You can adjust the character set in the regex to any delimiters you would like to find, just be careful to stick to the Regex syntax. Or, you can replace the example expression with any formula you would like the delimiter to be.

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