Please enable JavaScript.
Coggle requires JavaScript to display documents.
Creating and Manipulating Strings (String methods (equals() (compare…
Creating and Manipulating Strings
Concatenation
Operator +
Concatenate two strings or more
e.g "a" + "b" = ab;
rules
both numerics. addition
if either is String, concatenation
Evaluate left to right.
e.g 1 + 2 = 3
"a" + "b" = ab
"a" + "b" + 3 = ab3
1 + 2 + "c" = 3c
Tips
Trick on exam
e.g int three = 3;
String four = "4";
1 + 2 + three + four
result "64"
Being methodical and see every data type
String s1 = "1";
String s2 = s1.concat("2");
s2.concat("3");
s2 is 12, because concat return a new String
Immutable
Strings
String pool
Local in JVM
collects all literals strings
e.g String s = "Fluffly";
Create literal string into string pool
e.g String s = new String("Fluffly");
Dont create string into string pool
String methods
Index Strings starts in 0
length()
Return the number of characters
It is a sequence of characters
charAt(index)
Return a char in index
indexOf()
finds index that matches the value
no match found return -1
substring()
return string in starts index until end index
substring(startIndex, endIndex);
substring(startIndex);
startIndex greater than endIndex
throw StringIndexOutOfBoundException
toUpperCase() and toLowerCase()
do exactly they say
equals()
compare exactly strings
return true or false are differents
equalsIgnoreCase()
Ignore case, true if equals
startsWith(String) and endsWith(String)
starts prefix
ends suffix
contains(String)
match string in anywhere
return true if matches
replace(char old, char new);
replace(String old, String new);
replace a value for new value into string
trim()
removes whitespace from the beginning and end of string
Method chaining
String a = "abc";
String b = a.toUpperCase();
b = b.replace("B", "2").replace('C','3');
Sysout(a);//abc
Sysout(b);//A23
the replace is trickier