How to get the substring of a given String In Java?
String are most common data types used in any programming languages. In Java String is an Object and is backed by a character array.
For example, String str = "abc"; is equivalent to char val[] = {'a','b','c'};
As String is an object, it has convenient methods to manipulate the contents of String. Remember, Strings are immutable in Java, which means, when ever you perform an action, which requires it's content change, a new String object will be created.
Now, lets see which method, we can use to get sub string of the given String.
substring() :
substring() method is used to obtain a portion of string between two different positions. The substring() method is overloaded in java, means it has two different method signatures.
substring(int beginIndex) :
Use this method, when ever you want a portion of String, from the given index to end of the string. As Strings are index based, the 1st character will be at 0th position, 2nd character at 1st position and so on.
For example, given a string "speaking cs", if you want to get the sub string "king cs", you need to call substring(4). Even though 'k' is at position 5, we need to pass 4 as argument, as Strings are index based (starting from 0).
Let's see a working example.
Do remember, Here calling substring(4), will always returns a new String, so assign it to another String variable.
substring(int beginIndex, int end Index) :
Use this method, when ever you want a portion of String in the give range. For example, if you want only "king" in "speaking cs", call substring(4,8). This method, will give substring starting from index 4 up to 8. It won't include 8th character.
Let's see an example.
Note :The parameters, you are passing, to substring() method should be positive integers, and with in the string length only. Otherwise, you'll get StringIndexOutOfBoundException().
For example, String str = "abc"; is equivalent to char val[] = {'a','b','c'};
As String is an object, it has convenient methods to manipulate the contents of String. Remember, Strings are immutable in Java, which means, when ever you perform an action, which requires it's content change, a new String object will be created.
Now, lets see which method, we can use to get sub string of the given String.
substring() :
substring() method is used to obtain a portion of string between two different positions. The substring() method is overloaded in java, means it has two different method signatures.
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)
substring(int beginIndex) :
Use this method, when ever you want a portion of String, from the given index to end of the string. As Strings are index based, the 1st character will be at 0th position, 2nd character at 1st position and so on.
For example, given a string "speaking cs", if you want to get the sub string "king cs", you need to call substring(4). Even though 'k' is at position 5, we need to pass 4 as argument, as Strings are index based (starting from 0).
Let's see a working example.
package com.speakingcs.strings; public class SubstringExample { public static void main(String[] args) { String str = "speaking cs"; String subStr = str.substring(4); System.out.println(subStr); } }
Do remember, Here calling substring(4), will always returns a new String, so assign it to another String variable.
substring(int beginIndex, int end Index) :
Use this method, when ever you want a portion of String in the give range. For example, if you want only "king" in "speaking cs", call substring(4,8). This method, will give substring starting from index 4 up to 8. It won't include 8th character.
Let's see an example.
package com.speakingcs.strings; public class SubstringExample { public static void main(String[] args) { String str = "speaking cs"; String subStr = str.substring(4,8); System.out.println(subStr); } Output : king
Note :The parameters, you are passing, to substring() method should be positive integers, and with in the string length only. Otherwise, you'll get StringIndexOutOfBoundException().
This comment has been removed by a blog administrator.
ReplyDelete