4.3 String Basics and Common Methods
A Java String is an object that stores text. Unlike a char[], a String is immutable: once created, its characters do not change. Methods that seem to edit a string return a new string instead.
String name = "Leaflet";String indexes start at 0, just like array indexes.
System.out.println(name.charAt(0)); // L
System.out.println(name.charAt(6)); // tlength()
length() returns how many characters are in the string:
String word = "Java";
System.out.println(word.length()); // 4Use word.length() - 1 for the last valid index.
char last = word.charAt(word.length() - 1);Calling charAt(word.length()) is out of bounds. The valid indexes are 0 through word.length() - 1.
charAt(index)
charAt(index) returns the character at one position:
String code = "A17";
char first = code.charAt(0); // 'A'
char last = code.charAt(2); // '7'charAt is useful when you need to inspect characters one by one, such as checking whether a code starts with a letter or counting digits in a line of text.
substring(begin, end)
substring(begin, end) returns a new string from begin up to but not including end.
String text = "Leaflet";
String part = text.substring(0, 4); // "Leaf"The end index is exclusive. This makes lengths easy:
String text = "program";
String sub = text.substring(2, 5); // "ogr"
System.out.println(sub.length()); // 3The substring starts at index 2 and stops before index 5, so its length is 5 - 2.
You can also pass only begin:
String text = "filename.txt";
String extension = text.substring(8); // ".txt"isEmpty() and isBlank()
isEmpty() checks whether the string length is zero.
"".isEmpty(); // true
" ".isEmpty(); // falseisBlank() checks whether the string is empty or contains only whitespace.
"".isBlank(); // true
" ".isBlank(); // true
" Java ".isBlank(); // falseUse isBlank() when user input that contains only spaces should count as empty.
Common String methods
Java's String class is useful because it carries many methods with the text. Here is the method map you should start recognizing. The next two sections practice several of these in more detail.
Comparing strings
Use equals() for exact text equality:
"Java".equals("Java"); // true
"Java".equals("java"); // falseUse equalsIgnoreCase() when uppercase/lowercase should not matter:
"YES".equalsIgnoreCase("yes"); // trueUse compareTo() when you need ordering:
"apple".compareTo("banana"); // negative
"cat".compareTo("cat"); // 0
"dog".compareTo("cat"); // positiveThe exact number from compareTo() is less important than its sign: negative means "before", zero means "same", and positive means "after".
Searching strings
Use contains() when you only need a yes/no answer:
"error: timeout".contains("error"); // trueUse indexOf() to get the first position of a match:
"error warning error".indexOf("error"); // 0Use lastIndexOf() to get the last position:
"error warning error".lastIndexOf("error"); // 14When a search method cannot find the text, indexOf() and lastIndexOf() return -1.
Use startsWith() and endsWith() for prefixes and suffixes:
"Report.java".startsWith("Report"); // true
"Report.java".endsWith(".java"); // trueChanging text by returning new strings
Because strings are immutable, methods such as toLowerCase(), toUpperCase(), trim(), strip(), and replace() return new strings.
String raw = " Java Basics ";
String lower = raw.toLowerCase(); // " java basics "
String upper = raw.toUpperCase(); // " JAVA BASICS "
String clean = raw.trim(); // "Java Basics"
String modernClean = raw.strip(); // "Java Basics"
String dashed = clean.replace(" ", "-"); // "Java-Basics"Use trim() or strip() before comparing user input. Use toLowerCase() when commands should be case-insensitive.
Splitting and joining
split() cuts a string into a String[] array:
String line = "java,arrays,strings";
String[] parts = line.split(",");String.join() puts an array or list of strings back together:
String label = String.join(" / ", parts);
System.out.println(label); // java / arrays / stringsThis pair is common in small text-processing tasks: read one line, split it into pieces, clean each piece, and join the result for display.