Method substring() returns a new string that is a substring of given string. Java String substring() method is used to get the substring of a given string based on the passed indexes.
There are two variants of this method. In this guide, we will see how to use this method with the help of examples.
There are two variants of this method. In this guide, we will see how to use this method with the help of examples.
String substring() method variants
There are two ways we can use the substring() method –
1. When we pass only the starting index:
String substring(int beginIndex)
Returns the substring starting from the specified index i.e beginIndex and extends to the character present at the end of the string. For example – "Chaitanya".substring(2) would return "aitanya". The beginIndex is inclusive, that is why the character present at the index 2 is included in the substring. This method throws IndexOutOfBoundsException If the beginIndex is less than zero or greater than the length of String (beginIndex<0||> length of String).
2. When we pass both the indexes, starting index and end index:
String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex – 1. Thus the length of the substring is endIndex-beginIndex. In other words you can say that beginIndex is inclusive and endIndex is exclusive while getting the substring.
For example – "Chaitanya".substring(2,5) would return "ait".
In case the position needs to be dynamically calculated based on a character or String we can make use of the indexOf method:
string.indexOf() method returns the position of the first occurrence of a specified value in a string.
string.lastIndexOf() method returns the position of the last occurrence of a specified value in a string.
3
indexOf() function is match first occurance, LastindexOf() function is match last occurance
Both function are used for finding index in String or Array;
Example:
package com.cerotid.fundamentals.substring;
public class IndexOfAndLastIndexOf {
public static void main(String[] args) {
String str = "Hello planet earth, you are a great planet.";
int n = str.lastIndexOf("planet");
int n2 = str.indexOf("planet");
System.out.println("the value of n is: " + n);
QqqqqSystem.out.println("the value of n is: " + n2);
}
}
Output:
the value of n is: 36
the value of n is: 6
package com.cerotid.fundamentals.substring;
import static org.junit.Assert.assertEquals;
public class StringSubStringDemo {
public static void main(String[] args) {
String appliedDelimeter = "<>";
System.out.println("the length of appliedDelimeter is: "+appliedDelimeter.length());
String text = "Julia Evans was born on 25-09-1984. "
+ "She is currently living in the USA (United States of America).";
assertEquals("USA (United States of America).", text.substring(67));
assertEquals("USA (United States of America)", text.substring(67, text.length() - 1));
System.out.println("the lenght of string is: " + text.length());
System.out.println(text.substring(67));
// get rid of that extra dot at the end, in the example above:
System.out.println(text.substring(67, text.length() - 1));
// to calculate the position dynamically, use indexOf method:
assertEquals("United States of America", text.substring(text.indexOf('(') + 1, text.indexOf(')')));
System.out.println(text.substring(text.indexOf('(') + 1, text.indexOf(')')));
// A similar method that can help us locate our substring is lastIndexOf.
assertEquals("1984", text.substring(text.lastIndexOf('-') + 1, text.indexOf('.')));
System.out.println(text.substring(text.lastIndexOf('-') + 1, text.indexOf('.')));
System.out.println(text.lastIndexOf('-') + 1);
// Both indexOf and lastIndexOf can take a character or a String as a parameter
assertEquals("USA (United States of America)", text.substring(text.indexOf("USA"), text.indexOf(')') + 1));
System.out.println(text.substring(text.indexOf("USA"), text.indexOf(')') + 1));
}
}
Output:
the length of appliedDelimeter is: 2
the lenght of string is: 98
USA (United States of America).
USA (United States of America)
United States of America
1984
30
USA (United States of America)
No comments:
Post a Comment