A substring is part of a String. We can get a substring from String in Java in multiple ways. Here we will focus on methods from the String class and StringUtils class from the Apache Commons library.
Methods from the String class that we can use to get a substring from String in Java:
- public String substring(int startIndex) – returns the substring of the given String from specified startIndex (inclusive).
- public String substring(int startIndex, int endIndex) -returns the substring of the given string from specified startIndex to endIndex.
Using the substring() methods from the String class – examples
Example 1
public class Test { public static void main(String[] args) { String str = "Learn Java with alegrucoding.com"; // extract the substring starting from index 6 String substring = str.substring(6); // extract the substring from index 0 (inclusive) to 10 (exclusive) String substring2 = str.substring(0, 10); System.out.println(substring); System.out.println(substring2); } }
Output: Java with alegrucoding.com Learn Java
The substring() methods from the String class are very useful when we know the position of the Substring in a String.
Example 2
In case we don’t know the exact position of the substring in a String, we can use the indexOf() method to find out the start index of a substring.
Let’s extract the “Java” substring from the above String:
public class Test { public static void main(String[] args) { String str = "Learn Java with alegrucoding.com"; int startIndex = str.indexOf("Java"); // retrieve the start index of a substring String javaSubstring = str.substring(startIndex, startIndex + 4); // we specify endIndex as startIndex plus the length of the substring that we want to extract System.out.println(javaSubstring); } }
Output: Java
(Note: we will not cover all of the methods here, for a full list, check out the official documentation – link provided above)
- public static String substringBefore(String str, String separator) – Gets the substring before the first occurrence of a separator.
- public static String substringAfter(String str, String separator) – Gets the substring after the first occurrence of a separator.
- public static String substringBetween(String str, String tag) – Gets the String that is nested in between two instances of the same String.
- public static String substringBetween(String str, String open, String close) – Gets the String that is nested in between two Strings.
Using the methods from the StringUtils class
Example
import org.apache.commons.lang3.StringUtils; public class Test { public static void main(String[] args) { String str = "Hello, my name is Tom, and I live in USA"; String substringBefore = StringUtils.substringBefore(str, ","); String substringAfter = StringUtils.substringAfter(str, ","); String substringBetween = StringUtils.substringBetween(str, ","); String substringBetween2 = StringUtils.substringBetween(str, "name is ", ","); System.out.println(substringBefore); System.out.println(substringAfter); System.out.println(substringBetween); System.out.println(substringBetween2); } }
Output: Hello my name is Tom, and I live in USA my name is Tom Tom
Note: To use the StringUtils class, add the dependency to your project’s pom file.
That’s it!
Happy coding!