16.7. Extracting Substrings from StringsClass String provides two Substring methods, which are used to create a new String by copying part of an existing String. Each method returns a new String. Figure 16.6 demonstrates both methods. Figure 16.6. Substrings generated from Strings.
Line 11 uses the Substring method that takes one Integer argument. The argument specifies the starting index from which the method copies characters in the original String. The substring returned contains a copy of the characters from the starting index to the end of the String. If the index specified in the argument is outside the bounds of the String, an ArgumentOutOfRangeException occurs. The second version of method Substring (line 15) takes two Integer arguments. The arguments specify the starting index from which to copy characters and the length of the substring to copy. The substring returned contains a copy of the specified characters from the original String. If the starting index plus the length is greater than the number of characters in the String, an ArgumentOutOfRangeException occurs. |