Which methods are used to extract the characters from String?
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. public: System::String ^ Substring(int startIndex);public string Substring (int startIndex);member this.Substring : int -> stringPublic Function Substring (startIndex As Integer) As StringParametersstartIndex Int32 The zero-based starting character position of a substring in this instance. ReturnsStringA string that is equivalent to the substring that begins at startIndex in this instance, or Empty if startIndex is equal to the length of this instance. ExceptionsstartIndex is less than zero or greater than the length of this instance. ExamplesThe following example demonstrates obtaining a substring from a string. using namespace System; using namespace System::Collections; int main() { arrayThe following example uses the Substring method to separate key/value pairs that are delimited by an equals ("=") character. String[] pairs = { "Color1=red", "Color2=green", "Color3=blue", "Title=Code Repository" }; foreach (var pair in pairs) { int position = pair.IndexOf("="); if (position < 0) continue; Console.WriteLine("Key: {0}, Value: '{1}'", pair.Substring(0, position), pair.Substring(position + 1)); } // The example displays the following output: // Key: Color1, Value: 'red' // Key: Color2, Value: 'green' // Key: Color3, Value: 'blue' // Key: Title, Value: 'Code Repository' let pairs = [| "Color1=red"; "Color2=green"; "Color3=blue" "Title=Code Repository" |] for pair in pairs do let position = pair.IndexOf "=" if position >= 0 then printfn $"Key: {pair.Substring(0, position)}, Value: '{pair.Substring(position + 1)}'" // The example displays the following output: // Key: Color1, Value: 'red' // Key: Color2, Value: 'green' // Key: Color3, Value: 'blue' // Key: Title, Value: 'Code Repository' Module Example Public Sub Main() Dim pairs() As String = { "Color1=red", "Color2=green", "Color3=blue", "Title=Code Repository" } For Each pair In pairs Dim position As Integer = pair.IndexOf("=") If position < 0 then Continue For Console.WriteLine("Key: {0}, Value: '{1}'", pair.Substring(0, position), pair.Substring(position + 1)) Next End Sub End Module ' The example displays the following output: ' Key: Color1, Value: 'red' ' Key: Color2, Value: 'green' ' Key: Color3, Value: 'blue' ' Key: Title, Value: 'Code Repository'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 a 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 startIndex position in the current string. 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 startIndex. The second example illustrates this; it extracts a key value that begins one character position after the "=" character. If startIndex is equal to zero, the method returns the original string unchanged. 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. public: System::String ^ Substring(int startIndex, int length);public string Substring (int startIndex, int length);member this.Substring : int * int -> stringPublic Function Substring (startIndex As Integer, length As Integer) As StringParametersstartIndex 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 length that begins at startIndex in this instance, or Empty if startIndex is equal to the length of this instance and length is zero. ExceptionsstartIndex plus length indicates a position not within this instance. -or- startIndex or length is less than zero. 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). String value = "This is a string."; int startIndex = 5; int length = 2; String substring = value.Substring(startIndex, length); Console.WriteLine(substring); // The example displays the following output: // is let value = "This is a string." let startIndex = 5 let length = 2 let substring = value.Substring(startIndex, length) printfn $"{substring}" // The example displays the following output: // is Module Example Public Sub Main() Dim value As String = "This is a string." Dim startIndex As Integer = 5 Dim length As Integer = 2 Dim substring As String = value.Substring(startIndex, length) Console.WriteLine(substring) End Sub End Module ' The example displays the following output: ' isThe 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 ("=") character. 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 a 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 length characters starting from the startIndex position in the current string. The length parameter represents the total number of characters to extract from the current string instance. This includes the starting character found at index startIndex. In other words, the Substring method attempts to extract characters from index startIndex to index startIndex + length - 1. 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 startIndex. If the substring extends from startIndex to a specified character sequence, you can call a method such as IndexOf or LastIndexOf to get the index of the ending character or character sequence. You can then convert that value to an index position in the string as follows:
If startIndex is equal to zero and equals the length of the current string, the method returns the original string unchanged. See also
Applies toHow do I extract a character from a string in Java?Using String.. Get the string and the index.. Create an empty char array of size 1.. Copy the element at specific index from String into the char[] using String. getChars() method.. Get the specific character at the index 0 of the character array.. Return the specific character.. Which function is needed to extract a character from a string in Java?In Java, the charAt() function returns a character at the specified index of a string.
What are the methods of strings?Java String Methods. Java String split() Splits the string at the specified string (regex). Java String compareTo() Compares two strings in the dictionary order.. Java String compareToIgnoreCase() ... . Java String length() ... . Java String replace() ... . Java String replaceAll() ... . Java String substring() ... . Java String equals(). What are the string 5 methods?Let's dive deep into the most commonly used Java string methods and understand their working.. indexOf() Used to find characters and substrings in a string. ... . toCharArray() Used to form a new character array from this string. ... . charAt() ... . concat() ... . replace() ... . substring() ... . split() ... . compareTo(). What is string extraction in Java?Java String class provides the built-in substring() method that extract a substring from the given string by using the index values passed as an argument. In case of substring() method startIndex is inclusive and endIndex is exclusive. Suppose the string is "computer", then the substring will be com, compu, ter, etc.
|