- length():
returns the length of the string
Examples:
String s = "Abc", t = "Hello";
s.length() returns 3
t.length() returns 5
|
- charAt(n):
returns the charcater
at position n in the string
Examples:
012 01234
String s = "Abc", t = "Hello";
s.charAt(1) returns 'b'
t.charAt(1) returns 'e'
|
- substring(i,j):
returns the substring
from position i
upto (not including)
position j of the string
Examples:
012 01234
String s = "Abc", t = "Hello";
s.substring(0,2) returns "Ab"
t.substring(1,4) returns "ell"
|
- Concatenating strings:
- Using an instance method
(defined inside the
String class):
String s = "Abc", t = "Hello";
s.concat("xyz") returns "Abcxyz"
t.concat(s) returns "HelloAbc"
Note: We would normally store the return value in a String variable
String r;
r = t.concat(s); // r = "HelloAbc"
|
- The designers
of the Java language wanted to
treat Strings special
and they made the Java compiler recognize a
short hand notation
for String concatenation:
String s = "Abc", t = "Hello";
s + "xyz" // Synonym for: s.concat("xyz")
t + s // Synonym for: t.concat("s)
Note: We would normally store the return value in a String variable
String r;
r = t + s // Same as: r = t.concat(s);
|
|
|