Improve Article Save Article String is a special class in Java. substring() is one of the widely used methods of String class. It is used to extract part of a string and has two overloaded variants: 1. substring(int beginIndex): This method is used to extract a portion of the string starting from beginIndex. Example:
The beginIndex parameter must be within the range of source string, otherwise you would see the following exception:
2. substring(int beginIndex, int endIndex): This variant accepts two parameters beginIndex and endIndex. It breaks String starting from beginIndex till endIndex – 1. Example:
How substring() works internally
When we take substring from original string, new String object will be created in constant pool or in heap. The value[] char array will be shared among two String objects, but count and offset attributes of String object will vary according to substring length and starting index.
Problem caused by substring() in JDK 6 Example:
The above String already occupies a lot of memory in heap. Now consider the scenario where we need first 2 characters from bigString,.
Now we don’t need the original String.
We might think that bigString object will be Garbage collected as we made it null but our assumption is wrong. When we call substring(), a new String object is created in memory. But still it refers the char[] array value from original String. This prevents bigString from Garbage collection process and we are unnecessarily storing 100000 bytes in memory (just for 2 characters). The bug details can be found here. Handling substring() in JDK 6
Now, new String object is created in java heap, having its own char[] array, eventually original bigString will eligible for garbage collection process. Other option is, call intern() method on substring, which will then fetch an existing string from pool or add it if necessary.
Fix for substring() in JDK 7
It is worth noting that, new String object from memory is referred when substring() method is invoked in JDK 7, thus making original string eligible for garbage collection. Java String substring() method returns the substring of this string. This method always returns a new string and the original string remains unchanged because String is immutable in Java. Java String substring method is overloaded and has two variants.
Here is a simple program for the substring in java. package com.journaldev.util; public class StringSubstringExample { public static void main(String[] args) { String str = "www.journaldev.com"; System.out.println("Last 4 char String: " + str.substring(str.length() - 4)); System.out.println("First 4 char String: " + str.substring(0, 4)); System.out.println("website name: " + str.substring(4, 14)); } }Output of the above substring example program is: Last 4 char String: .com First 4 char String: www. website name: journaldevWe can use the substring() method to check if a String is a palindrome or not. package com.journaldev.util; public class StringPalindromeTest { public static void main(String[] args) { System.out.println(checkPalindrome("abcba")); System.out.println(checkPalindrome("XYyx")); System.out.println(checkPalindrome("871232178")); System.out.println(checkPalindrome("CCCCC")); } private static boolean checkPalindrome(String str) { if (str == null) return false; if (str.length() <= 1) { return true; } String first = str.substring(0, 1); String last = str.substring(str.length() - 1); if (!first.equals(last)) return false; else return checkPalindrome(str.substring(1, str.length() - 1)); } }Here we are checking if the first letter and the last letter is the same or not. If they are not the same, return false. Otherwise, call the method again recursively passing the substring with the first and last letter removed. You can checkout more string examples from our GitHub Repository. Reference: Oracle API Doc Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest. Sign up |