The String
class is immutable: once it’s created, a String
object can’t be changed. Therefore its methods never affect the original String
object but create and return a new String
object that contains the result of the operation.
In order to correctly answer questions in the exam about strings you must know the String
class main methods and what’s the behaviour of these methods.
Basic Operations
char charAt(int index) |
Returns the character at the given index |
String trim() |
Returns a copy of the string with no leading and trailing whitespace |
String concat(String s2) |
Returns a new string (this + s2 ) |
Examples:
1 2 3 4 5 6 7 8 9 10 11 |
String s = new String("12345"); s.concat("678"); // s is not affected by this operation System.out.println(s); // prints "12345" s = s.concat("678"); // s is assigned the result of the concatenation System.out.println(s); // prints "12345678" char c = s.charAt(5); // same as arrays, String indexes are 0-based System.out.println(c); // prints "6" |
Getting Portions
String substring(int begin[, int end]) |
Returns a new string containing characters from begin to the end of this [or to the character end-1 ] |
CharSequence subSequence(int begin, int end) |
Same as substring but returns a CharSequence object |
String[] split(String regex[, limit]) |
Splits the string around matches of the given regular expression (limit is the max size of the array) |
Example:
1 2 3 4 5 |
String s = "12345678"; s = s.substring(2, 5); // char at index 2 is "3", at index 5 is "6" System.out.println(s); // prints "345" |
Replacing Characters
String replace(char oldChar, char newChar) |
Replaces all occurrences of the character oldChar with newChar |
String replaceAll(String regex, String replacement) |
Replaces each substring that matches the regular expression regex with replacement |
String replaceFirst(String regex, String replacement) |
Same as replaceAll but only replaces the first occurrence of regex |
Example:
1 2 3 4 5 |
String s = "01234567890123456789"; s = s.replace("6", "X"); // replaces all the occurrences of "6" in s System.out.println(s); // prints "012345X789012345X789" |
Comparing Strings
boolean startsWith(String prefix)
|
Checks if a string starts/end with the given prefix/suffix |
boolean equals(Object anObject) |
Checks if this string has the same sequence of characters as the given string parameter |
boolean equalsIgnoreCase(String anotherString) |
Same as equals but ignores case considerations (e.g "abc" equals to "AbC" ) |
int compareTo(String anotherString)
|
Compares two strings lexicographically (e.g. "A" > "a" , "a" > "b" ) |
boolean matches(String regex) |
Checks if a string matches a given regular expression |
Strings and Primitive Data Types
There are different ways of converting primitive data types to String
objects:
1 2 3 4 5 6 7 8 9 10 11 |
// 1. converting with the + operator String s = "" + i; // 2. converting with String static methods String s = String.valueOf(i); String s = String.format("Value = %d", i); // 3. converting with wrapper class static methods String s = Integer.toString(i); |
For converting String
to primitive data types, see the methods in the wrapper classes.
2 comments
Hey Raul, shouldn’t the line “String.format(“Value = %d” + i);” look like “String.format(“Value = %d”, i);” instead?
You’re right, Bartosz. I just corrected it.