Substring IndexOf in c#

Viewed 16477

I have a string that looks like this: "texthere^D123456_02". But I want my result to be D123456.

this is what i do so far:

if (name.Contains("_"))
{
name = name.Substring(0, name.LastIndexOf('_'));
}

With this I remove at least the _02, however if I try the same way for ^ then I always get back texthere, even when I use name.IndexOf("^")

I also tried only to check for ^, to get at least the result:D123456_02 but still the same result.

I even tried to name.Replace("^" and then use the substring way I used before. But again the result stays the same.

texthere is not always the same length, so .Remove() is out of the question.

What am I doing wrong?

Thanks

13 Answers
Related