#102 – Use Substring() to Extract Substrings From A String
September 27, 2010 Leave a comment
The String.IndexOf method searched for the location of a substring in a larger string, given the substring. You can use the Substring method to do the reverse–extract a substring, given its position in the larger string.
string s = "Itsy bitsy spider"; string s2 = s.Substring(5); // bitsy spider (start at 6th character, extract until end of string) s2 = s.Substring(5, 4); // bits s.Substring(2, s.Length); // throws ArgumentOutOfRangeException
Also note that the Substring method doesn’t change the original string, but just returns the substring asked for.