substring : Java Glossary
home S words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
substring
String.substring(int start, int end) is Java’s way of making a copy of a piece of a String. Note it is spelled out substring not the usual substr. Java is different from most other languages in that you specify the end point, not the length of the substring. The offsets are 0-based, i.e. the first character of the String is character 0. To further confuse you, the end points one character past the end of the String. It is perhaps best to think of it this way. Imagine little vertical bars separating the characters of the String, with a bar on the beginning and end of the String as well. You feed substring the start and end vertical bar numbers that enclose the substring you want.

0_1_2_3_4_5
|h|e|l|l|o|
0_1_2_3_4_5

"hello".substring( 1 ,3 ) == "el";
substring is clever. It does not make a deep copy of the substring the way most languages do. It just creates a pointer into the original immutable String, i.e. points to the value char[] of the base string, and tracks the starting offset where the substring starts and count of how long the substring is. This could be confusing if you were low-level debugging since you would see the whole String, not just the substring. There were reports of a bug in Microsoft’s implementation of substring. The downside of this cleverness is a tiny substring of a giant base String could suppress garbage collection of that big String in memory even if the whole String were no longer needed. (actually its value char[] array is held in RAM; the String object itself could be collected.)

It is probably still a good idea to use indexOf( lookFor, offset ) rather than creating a substring first and using indexOf( lookFor ) on that.

If you know a tiny substring is holding a giant string in RAM, that would otherwise be garbage collected, you can break the bond by using littleString = new String( littleString ) which will create a new smaller backing char[] with no ties to the original String.

If you are a curious sort, and study the code for String. substring in src.zip, this sharing logic might not be apparent. The key is a non-public String constructor that takes parameters in the reverse of the usual order String (int offset, int count, char value[]).


CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] Visit Western Canada Wilderness Committee
You are visitor number 131,732.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/substring.html J:\mindprod\jgloss\substring.html