To search within a string in C#, the IndexOfAny(Char [])
and LastIndexOfAny(Char[]);
IndexOfAny(Char)
Returns the zero-based index position of the first occurrence in this instance of any one or more characters specified in a Unicode character array.
LastIndexOfAny(Char[])
does the same, only returns the index of the last occurrence.
When the substring is not found, the methods return -1.
string welcome = "Hello world! Goodbye world!"
int x = welcome.IndexOfAny(new Char[] {'w'}); // 6
Please note: these methods do not look for the number of occurrences, but only determine whether there is such a substring in the string or not.
To remove a substring, you can use the Remove(Int32, Int32)
method - it removes the substring from the first specified index to the second.
string welcome = "Hello world! Goodbye world!";
string cut = welcome.Remove(1, 3); // "Ho world! Goodbye world!"