Some
methods below: .length( ), .substring(...), indexOf(...), charAt(...), concat(...),
equalsIgnoreCase(...), replace(...),
trim( )
for getChars(...) see StringBuffer’s .getChars(...) method
■ Strings are immutable, period. Whenever it looks like you’re altering a String you’re really getting a new object.
■ String allows both + and +=
operators.
■ When using the += for appending, you don’t really alter the
String. A new (longer) object is created and the old handle is pointed to it.
■ When using .concat(...) for
appending, a new (longer) object is created but the old handle IS NOT pointed
to it. So you need to make an assignment.
i.e. After the following code
both String
variables s1 and s2 still contain contents of "One":
String s1 =
"One";
String s2
="Two";
s1.concat("More");
s2 = s1;
(Note that adding an assignment
saying s1 = s1.concat(" More"); fixes this problem.)
String Part2 =
"Part2";
String s =
"Part1"; //
Does fix it
s += Part2;
System.out.println(s);
String t =
"Part1"; //
Doesn't
t.concat(“Part2”);
System.out.println(t);
String u = "Part1";
u =
u.concat("Part2"); //
Does fix it
System.out.println(u);
■ If the
above example is used with the .append(...) method and a StringBuffer, StringBuffer does
update the pointer and hence the result would
contain "One More." i.e.
StringBuffer s1 = new
StringBuffer("One");
s1.append("More");
System.out.println(s1);
■ String has
an equals( )
method override whereas StringBuffer does
not.
So, for String, == is
not the same as .equals( ). .equals( ) will
compare the contents.
For StringBuffer, == is
the same as .equals( ).
■ Strings are
entered as 8-bit ASCII, but immediately become 16-bit Unicode.
■ All the println(...) methods
expect a String or
something that can be converted to one.
i.e.
System.out.println(“” +
null); works and prints null
However
System.out.println(null); won’t compile
■ You can
directly include escape sequences in String values. i.e
String
s = “WORDS \n”; works.
■ If you
know the codes, you can use the \uffff 4-digit escape sequence to represent
characters. Double quotes are
necessary. i.e.
String letterA =
“\u00c1”; works.
■ A print(...)
method needs an intial String term
to which to concatenate. i.e.
char c1 = 1;
char c2 = 2;
System.out.print(c1 +
c2); becomes a legal
math addition.
b1 = 1;
b2 = 2;
b3 = 3;
System.out.println(b1 +
b2 + b3); prints 6
System.out.println(“”
+ b1 + b2 + b3); prints 123
■ You can’t
begin a System.out.print(...) with
a Boolean or a
boolean. You need a “” +
first, to start the conversions. i.e.
System.out.prnt( bool1 +
bool2); doesn’t compile
because it thinks it’s an illegal addition operation.
System.out.prnt( “” +
bool1 + bool2); works because of
the empty “” starter.
■ You can't
create a null String with
the shorthand construction. i.e.
String s = new
String(null); won’t
compile
String s = new
String(“”); works however and gives a valid s.length(
) of zero.
String s = “”; works
similarly and s.length( ) is again zero.
■ You can
use a String’s .length(
) method only after creation. It
gives a compile error if used after just declaration.
■ You can't
use .length( )
against a null Sting. After s = null; then saying s.length( ) will give a NullPointerException.
■ You can’t ever use length( ) to assign a length. i.e.
s.length( ) = 0; won’t compile
String .substring( index ) method
■ Note that
this method name does NOT have a Capital “S” inside it.
■ substring(...) requires that an index
parameter be present or you get a compile error. Its index starts at location 0.
.substring( int ) returns from the index to the end.
.substring( int, int ) returns from the index to the ending index
position minus
1 . i.e. This
snippet retuns just the word “lamb”.
String s = "Mary had
a little lambie.";
String s2 =
s.substring(18, 22);
System.out.println ( s2
);
■ A
substring is not an Array. It does not give an index out of bounds exception if
it goes off the end to the right. It just returns null.
■ It will
compile but then will give an index out of bounds exception if it is passed a negative
index.
int .indexOf( substring
) method
■ Works the
same for both String and Stringbuffer.
■ Searching a String or StringBuffer. Returns the int index of the start of the first occurrence of the searched-for substring, or, returns –1 if not found. The returned index starts at zero. i.e. the snippet prints the index of the y in Mary, which is a location 3, then prints 13 which is the index of the tt.
String s = "Mary had
a little lamb.";
System.out.println("Index of the y in Mary is: " + s.indexOf("y"));
System.out.println("Index of the tt is: " + s.indexOf("tt"));
■ The same for both String and Stringbuffer.
■ Returns the char at the desired index location. Index starts at zero. Will throw an index out of bounds exception.
■ Note that finding the index position (indexOf(...) ) is quite different from obtaining the actual character (charAt(...) ).
String
.concat( String )
method
■ Attaches the parm String to the end and returns a new, lengthier, object. i.e.
x.concat(“more”); Note that x isn’t updated to point to new
object. See discussion at top of this page.
boolean .equalsIgnoreCase( 2ndStr
) method
■ Returns true if the argument is not null and the Strings are equal.
String
.replace( oldchar, newchar ) method
■ Returns a new object with the chars replaced. Note it takes chars not Strings.
■ If the oldChar does not occur, then a reference to the original String is returned. Otherwise, a new object is created
■ Doesn't
update the object's pointer with s.replace('A', 'B'); Needs an assignment like s
= s.replace('A', 'B');
■ Returns a new String with the front and back whitespaces and control characters stripped off.
■ Doesn't update the original object's pointer with s.trim( ); Needs s = s.trim( );