MoShe
MoShe

Reputation: 6427

First occurrence of one item from given string array

I am lookin for way to get the index of the first occurrence of one item from giveמ string array:

for example:

I have the following array:

 string[] dlimiterArray =  { ":", ".", ",", " ", "-", ";" };

and also have the following string:

string data = "hi hi bla bla bla user:myusername. bla bla bla";

and I would like to do get the first time that one of the dlimiterArray items Appears

Upvotes: 0

Views: 244

Answers (1)

Ry-
Ry-

Reputation: 224859

Use String.IndexOfAny:

int index = data.IndexOfAny(dlimiterArray);

However, you'll also want to change dlimiterArray to a char[], not a string[]:

char[] dlimiterArray =  { ':', '.', ',', ' ', '-', ';' };

Upvotes: 4

Related Questions