Java String substring() Method Last Updated : 11 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice In Java, the substring() method of the String class returns a substring from the given string. This method is most useful when you deal with text manipulation, parsing, or data extraction.This method either takes 1 parameter or 2 parameters, i.e., start and end value as arguments.In case the end parameter is not specified, then the substring will end at the end of the string.Syntax of String substring() Methodpublic String substring(int beginIndex);public String substring(int beginIndex, int endIndex);Parameters:beginIndex: Starting index of a substring where we want to specify the starting point of a string, and it is inclusive.endIndex(Optional): The ending index of the substring It is exclusive. If we don't specify the endIndex, the substring will end at the end of the string.Return Type: String: It returns a new String object containing which contains the specified substring with the specified index range.Exceptions:StringIndexOutOfBoundsException: This exception will happen when the index value is in cases such as negative beginIndex or an endIndex is greater than the length of the string or beginIndex is greater than the endIndex.Note: If we pass the start index and endIndex with the same value so it will return an empty substring ("").Example 1: Java program to extract a substring using substring(int beginIndex). Java // Java program to show the use of // substring(int begIndex) public class Geeks { public static void main(String[] args) { String s = "GeeksforGeeks"; // Extracting substring starting from index 8 String sub = s.substring(8); // printing the substring System.out.println("Substring: " + sub); } } OutputSubstring: Geeks Explanation: In the above code example, we use the substring(int beginIndex) method and specify the start index which is 8 and it didn't specified the end index. So it will make substring from end of the given string and we get the substring as "Geeks".Diagramatic Representation:Java SubstringExample 2: Java program to extract a substring from specified start index to specified end index using substring(int beginIndex, int endIndex). Java // Java program to show the use of // substring(int begIndex, int endIndex) public class Geeks { public static void main(String[] args) { String s = "Welcome to GeeksforGeeks"; // Extract substring from index 0(inclusive) // to 7 (exclusive) String sub = s.substring(0, 7); // printing the substring System.out.println("Substring: " + sub); } } OutputSubstring: Welcome Explanation: In the above example, we use the substring(beginIndex, endIndex) the substring starts at index 0 and ends at index 7, but the character at index 7 is excluded. Comment More infoAdvertise with us Next Article Java String substring() Method A Astha Tyagi Follow Improve Article Tags : Java Java-Strings Java-lang package Java-Functions Java-String-Programs +1 More Practice Tags : JavaJava-Strings Similar Reads Java String trim() Method The trim() method of the String class in Java is used to remove leading and trailing whitespace from a string. The Unicode value of the space character is "\u0020". It does not remove any whitespace in the middle of the string. This method returns a new string with the whitespace removed and leaves 4 min read Java String split() Method The String split() method in Java is used to split a string into an array of substrings based on matches of the given regular expression or specified delimiter. This method returns a string array containing the required substring.Example: Here, we will split a String having multiple delimiters or re 6 min read StringJoiner toString() method in Java The toString() of StringJoiner is used to convert StringJoiner to String. It returns the current value, consisting of the prefix, the values added so far separated by the delimiter, and the suffix, unless no elements have been added in which case, the prefix + suffix or the emptyValue characters are 2 min read StringBuffer substring() method in Java with Examples In StringBuffer class, there are two types of substring method depending upon the parameters passed to it. substring(int start) 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. The strin 4 min read Object toString() Method in Java Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class, henceforth, it is a child of the Object class. If a class does not extend any other class then it is a direct child class of Object, and if it extends another class, then it is 3 min read Java String subSequence() Method with Examples In Java, the String class provides the subSequence() method. This subSequence() method extracts a portion of a string as a CharSequence. This method is useful for retrieving specific character ranges from a string which enhances string manipulation. In this article, we will learn how to use the subS 3 min read StringJoiner length() method in Java The length() of StringJoiner is used to find out the length of the StringJoiner in characters. It returns the length of the String representation of this StringJoiner. Note that if no add methods have been called, then the length of the String representation (either prefix + suffix or emptyValue) wi 2 min read StringBuilder toString() method in Java with Examples The toString() method of the StringBuilder class is the inbuilt method used to return a string representing the data contained by StringBuilder Object. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by toString( 3 min read Java String.copyValueOf() Method The copyValueOf() method in Java is used to create a new String by copying characters from a character array. It is a static method of the String class.Variants: There are two variants of this method.Copy Entire Character ArrayCopy Subarray1. Copy Entire Character ArrayThis method copies the entire 2 min read String Class stripTrailing() Method in Java with Examples This method is used to strip trailing whitespaces from the string i.e. stripTrailing() method removes all the whitespaces only at the ending of the string. Example:Input: String name = " kapil "; System.out.println("#" + name + "#"); System.out.println("#" + name.stripTrailing() + "#"); Output: # ka 2 min read Like