Finding a string within a string

suggest change

Using the System.String.Contains you can find out if a particular string exists within a string. The method returns a boolean, true if the string exists else false.

string s = "Hello World";
bool stringExists = s.Contains("ello");  //stringExists =true as the string contains the substring

Using the System.String.IndexOf method, you can locate the starting position of a substring within an existing string.

Note the returned position is zero-based, a value of -1 is returned if the substring is not found.

string s = "Hello World";
int location = s.IndexOf("ello"); // location = 1

To find the first location from the end of a string, use the System.String.LastIndexOf method:

string s = "Hello World";
int location = s.LastIndexOf("l"); // location = 9

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


String Manipulation:
* Finding a string within a string

Table Of Contents
13 String Manipulation
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script