Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. String.Substring Method
DefinitionRetrieves a substring from this instance. This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list. In this articleOverloads
Substring(Int32)Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
ParametersstartIndex Int32 The zero-based starting character position of a substring in this instance. ReturnsStringA string that is equivalent to the substring that begins at Exceptions
ExamplesThe following example demonstrates obtaining a substring from a string.
The following example uses the Substring method to separate key/value pairs that are delimited by an equals (
The IndexOf method is used to get the position of the equals character in the string. The call to the Substring(Int32, Int32) method extracts the key name, which starts from the first character in the string and extends for the number of characters returned by the call to the IndexOf method. The call to the Substring(Int32) method then extracts the value assigned to the key. It starts at one character position beyond the equals character and extends to the end of the string. RemarksYou call the Substring(Int32) method to extract a substring from a string that begins at a specified character position and ends at the end of the string. The starting character position is zero-based; in other words, the first character in the string is at index 0, not index 1. To extract a substring that begins at a specified character position and ends before the end of the string, call the Substring(Int32, Int32) method. Note This method does not
modify the value of the current instance. Instead, it returns a new string that begins at the To extract a substring that begins with a particular character or character sequence, call a method such as IndexOf or
IndexOf to get the value of If See also
Applies toSubstring(Int32, Int32)Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
ParametersstartIndex Int32 The zero-based starting character position of a substring in this instance. length Int32 The number of characters in the substring. ReturnsStringA string that is equivalent to the substring of length Exceptions
-or-
ExamplesThe following example illustrates a simple call to the Substring(Int32, Int32) method that extracts two characters from a string starting at the sixth character position (that is, at index five).
The following example uses the Substring(Int32, Int32) method in the following three cases to isolate substrings within a string. In two cases the substrings are used in comparisons, and in the third case an exception is thrown because invalid parameters are specified.
The following example uses the Substring method to separate key/value pairs that are delimited by an equals (
The IndexOf method is used to get the position of the equals character in the string. The call to the Substring(Int32, Int32) method extracts the key name, which starts from the first character in the string and extends for the number of characters returned by the call to the IndexOf method. The call to the Substring(Int32) method then extracts the value assigned to the key. It starts at one character position beyond the equals character and extends to the end of the string. RemarksYou call the Substring(Int32, Int32) method to extract a substring from a string that begins at a specified character position and ends before the end of the string. The starting character position is zero-based; in other words, the first character in the string is at index 0, not index 1. To extract a substring that begins at a specified character position and continues to the end of the string, call the Substring(Int32) method. Note This method does not modify the value of the current instance. Instead, it returns a new string with The To extract a substring that begins with a
particular character or character sequence, call a method such as IndexOf or LastIndexOf to get the value of If the substring should extend from
If See also
Applies toAdditional resourcesIn this articleWhich method of class string is used to extract a substring from a string object?The substring(int start) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to end of this sequence.
Which of these methods of class StringBuffer is used to extract a substring from a string object Mcq?Which of these methods of class StringBuffer is used to extract a substring from a String object? Explanation: None.
Which of this method of class 10th is used to extract a substring from a string object?The substr() method extracts a part of a string.
What string method can be used to return a string from the given string?Public String substring(int startIndex, int endIndex):
This method is used to return a new String object that includes a substring of the given string with their indexes lying between startIndex and endIndex.
|