Java Glossary : substring

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 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

You are here : home : Java Glossary : S words : substring.

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 substr 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" ;

Substr 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. This can be confusing if you are low-level debugging since you will see the whole string. There have been reports of a bug in Microsoft's implementation of substr.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 13437.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/substring.html J:\mindprod\jgloss\substring.html