String is Java's standard string type from java.lang, so it needs no import. It manages its character count and storage automatically, making it the usual choice for text. Java strings are immutable: operations that appear to edit a string return a new string instead.
Create, read, and print
A string can be initialized from text and changed by assignment. scanner.next() reads one word; use scanner.nextLine() when spaces must be preserved.
String language = "Java";
String topic = scanner.nextLine();
System.out.println(language + ": " + topic);Length and state
length() returns the number of characters as an int. isEmpty() reports whether the string contains no characters. Neither operation changes the string.
String text = "Leaflet";
System.out.println(text.length()); // 7
System.out.println(text.isEmpty()); // falseAccess individual characters
charAt(index) accesses a character with a zero-based index. The first character is at index 0 and the final character is at text.length() - 1. It checks bounds and throws StringIndexOutOfBoundsException for an invalid index.
String text = "Leaflet";
System.out.println(text.charAt(0)); // L
System.out.println(text.charAt(text.length() - 1)); // tAppend and insert
String objects cannot be modified in place. Use + or concat() to create a new string. When repeated append or insert operations are needed, use a mutable StringBuilder.
String text = "Leaf";
text = text.concat("let"); // Leaflet
text += " course"; // Leaflet course
StringBuilder builder = new StringBuilder(text);
builder.insert(7, " Java"); // Leaflet Java course
text = builder.toString();Extract, find, and edit
substring(begin, end) returns a new string from begin up to, but not including, end. indexOf(value) returns the starting index of the first match, or -1 if no match exists. replace(old, replacement) also returns a new string.
String text = "Leaflet course";
String name = text.substring(0, 7); // Leaflet
int position = text.indexOf("course"); // 8
if (position != -1) {
text = text.replace("course", "lab"); // Leaflet lab
}
text = text.substring(0, 7); // LeafletJava String method workshop
Choose a method to inspect its call, whether the original String changes, and its returned or StringBuilder-edited character layout.
text.length();Returns the character count; the space counts too.
Compare strings
equals() and !equals() test content equality. compareTo() returns a negative value, zero, or a positive value when the current string sorts before, equals, or sorts after its argument. Do not use == to compare string contents.
String first = "apple";
String second = "banana";
if (!first.equals(second) && first.compareTo(second) < 0) {
System.out.println(first + " comes first");
}KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
Build a username
Read a one-word first and last name, build the String username “first.last”, and print it together with its length.
- Read first and last separately with Scanner.
- Concatenate first + "." + last.
- Print the username and username.length().
Extract an email domain
Read an email-shaped string and extract the domain after @, reporting an error when @ is absent or the domain is empty.
- Use indexOf to locate @.
- Check for -1 and ensure characters follow @.
- Use substring(position + 1) and print the domain.
Perform one text replacement
Read source text, a search fragment, and a replacement, then replace only the first occurrence.
- Use indexOf to obtain the first match position.
- When absent, keep the source unchanged and report it.
- When found, combine the text before and after the match with substring and print the result.