C#:Find Position

From Progzoo

You can find where one string occurs within another. If the substring occurs at the very start you'll get 0. If the substring is not there you get -1.

Find first occurance

Use s.IndexOf(t) to find where t occurs in s.

s.indexOf("t") gives the character position of the first "t" in s.

0123456789101112
One two three
four five six


[Font] [Default] [Show] [Resize] [History] [Profile]

Find last occurance

Use s.LastIndexOf(t) to find the last place where t occurs in s.

s.lastIndexOf("t") gives the character position of the last "t".


0123456789101112
One two three
four five six


[Font] [Default] [Show] [Resize] [History] [Profile]

Find occurance after ...

Use s.indexOf(t,n) to find where t occurs in s.

s.indexOf("t",5) gives the character position of the first "t" after position 5.


0123456789101112
One two three
four five six


[Font] [Default] [Show] [Resize] [History] [Profile]