Substring
The method substring() is used to get part of an existing string. This method is available in the classes
String, StringBuffer and StringBuilder. Methods in all the three classes do the same thing. So
understanding method in once class is enough to understand the behavior in the other two classes.
This method has 2 overloaded versions in these 3 classes.
String substring(int);
String substring(int, int);
substring(int startIndex)
this method gives a substring starting at the specified argument up to end of the string. The argument
should be an integer. The system would create a new String object using the characters from ‘startIndex’
up to end of the string. Suppose we have used the expression “computer”.substring(4), then new String
would be formed from 4th letter of “computer” (u) up to the last letter (r). So the new String would be
“uter”. [remember the index start from 0 but not from 1 in a String. So the character ‘c’ is at index 0 and
character ‘o’ is at index 1, character ‘m’ is at index 2 in the String “computer”]. Once the new String is
formed, there is no relation between the new String and the original string. Both are independent
strings.
Eg:
class Check
public static void main(String[] args)
String s="programming";
String t=s.substring(4);
String u=s.substring(8);
System.out.println(t);
System.out.println(u);
}
output:
ramming
ing
substring(int startIndex, int endIndex)
this method gives a substring starting at the specified first argument (startIndex) up to the specified
second argument (endIndex) excluding. The arguments should be integers. The system would create a
new String object using the characters from ‘startIndex’ up to (but not including) the endIndex. Suppose
we have used the expression “computer”.substring(3,7), then new String would be formed from 3 rd
letter of “computer” (p) up to the 6th letter (e). So the new String would be “pute”. [as the second
argument is 7, the new String is formed up to 6th. In case the second argument is 5, then the new string
is formed up to 4th ].
Eg:
class Check
public static void main(String[] args)
String s="programming";
String t=s.substring(4,8); // from 4th to 7th
String u=s.substring(2,5); // from 2nd to 4th
System.out.println(t); // ramm
System.out.println(u); // ogr
In any method, if we try to specify a location that does not exist, then an exception would be raised at
runtime (the program compiles normally).
Eg1:
String s="programming";
String u=s.substring(12); // StringIndexOutOfBoundsException is raised
Eg2:
String s="programming";
String t=s.substring(4,18); // would raise StringIndexOutOfBoundsException
The syntax of methods in the 3 classes can be seen below…
in String class
public String substring(int);
public String substring(int, int);
In StringBuilder class
public String substring(int);
public String substring(int, int);
in StringBuffer class
public synchronized String substring(int);
public synchronized String substring(int, int);
we can understand from the declarations in the three classes that all are same but StringBuffer methods
are synchronized. It means two threads cannot use these methods on a single String at a time. For a
normal (beginner) user (programmer), this is not a matter. So we can say all are same.
Along with substring, the String class has other methods like the following….
int codePointAt(int) String s="kanakesh";
System.out.println(s.codePointAt(1));//97
gives the integer equivalent (ascii/Unicode System.out.println(s.codePointAt(2));//110.
value) of the character at the specified
location.
int codePointBefore(int) String s="kanakesh";
System.out.println(s.codePointBefore(1));//107
gives the ascii value of character before the System.out.println(s.codePointBefore(2));//97
specified argument. If the specified System.out.println(s.codePointBefore(0));//exception
argument is 2, then ascii value of character
at 1 is given.
int codePointCount(int, int) String s="kanakesh";
System.out.println(s.codePointCount(0,4)); // 4
gives the count of characters in the given System.out.println(s.codePointCount(2,5)); // 3
range. The range is from startIndex
(including) to endIndex (excluding). The
startIndex is the first argument and
endIndex is the second argument
int compareTo(String) String s="kanakesh";
String t="goutham";
compares the invoking string with the String u="gowtham";
argumented string and returns the String v="gowthamkumar";
difference between them. The difference is String w="GOWTHAM";
an integer that represents the first System.out.println(s.compareTo(t)); // 4
(ascii/Unicode) difference between the two System.out.println(t.compareTo(s)); // -4
strings. System.out.println(t.compareTo(u)); // -2
Suppose we are comparing “kanakesh” and System.out.println(v.compareTo(u)); // 5
“goutham”, then starting letters (index 0) of System.out.println(u.compareTo(w)); // 32
the two strings are compared i.e. ‘k’ from
“kanakesh” and ‘g’ from “goutham”. As
these two are different characters, a
subtraction takes place from ‘k’ to ‘g’ (i.e.
107-103) and 4 will be the result.
Suppose we are comparing “goutham” and
“gowtham”, then characters at index 0 and
1 are same in both the strings and first
difference is found at index 2 (i.e. between
‘u’ and ‘w’). so the ascii values of the two
characters are used to find the difference
(i.e. 117-119) and -2 will be the result.
Suppose we are comparing
“gowthamkumar” and “gowtham”, the first
7 letters are matched (same). But after that
the second string has no more characters.
So the count of remaining characters (k, u,
m, a, r) is the result (i.e. 5)
Suppose we are comparing “gowtham” and
“GOWTHAM”, then the letters at index 0
mismatches. Because ‘g’ is lowercase letter
and ‘G’ is uppercase letter. So the difference
(103-71) is the result(i.e. 32)
int compareToIgnoreCase(String) String t="goutham";
String u="gowtham";
this is similar to compareTo() method but String v="1234567";
compares the two strings by ignoring the String w="GOWTHAM";
case differences. System.out.print(u.compareToIgnoreCase(w)); // 0
So “gowtham” and “GOWTHAM” will be System.out.print(t.compareToIgnoreCase(w)); // -2
treated as same by this method. System.out.print(u.compareToIgnoreCase(v)); // 54
System.out.print(w.compareToIgnoreCase(v)); // 54
System.out.print(w.compareToIgnoreCase("gOwThAm"));
// 0
int hashCode() String t="goutham";
String u="goutham";
returns the integer equivalent of the String v="gowtham";
HashCode generated for the String. String w="GOWTHAM";
System.out.println(t.hashCode());
System.out.println(u.hashCode());
System.out.println(v.hashCode());
System.out.println(w.hashCode());
Probable output:
213045165
213045165
214892207
932874895
int indexOf(int) String s="computerprogramming";
System.out.println(s.indexOf('c')); // 0
returns the location of the specified System.out.println(s.indexOf('m')); // 2
character in the String. If an integer value is System.out.println(s.indexOf('b')); // -1
given as argument, its equivalent System.out.println(s.indexOf(97)); // 13
ascii/Unicode character is considered.
If the specified character does not exist, -1 is
returned.
int indexOf(int, int) String s="computerprogramming";
System.out.println(s.indexOf('c',5)); // -1
returns the location of the specified System.out.println(s.indexOf('m',5)); // 14
character (first argument) from the specified System.out.println(s.indexOf('m',15)); // 15
location (second argument) of the String. If System.out.println(s.indexOf('m',16)); // -1
the specified character does not exist, -1 is
returned.