SwadhyAI Notes

All Java notes

Java String Methods Reference

›Short versionOpen if you only have a minute.
  • A String is a fixed sequence of UTF-16 code units. Nothing on this page edits that sequence in place.
  • trim, substring, replace, and toUpperCase return a new String. Keep the result or it is gone.
  • equals compares characters. == compares whether two variables point at the same object.
  • indexOf returns the first index, or -1 when the text is missing. -1 is not a valid index.
  • substring takes a start that is included and an end that is excluded. replace is literal; replaceAll and split use a regex.

String represents a sequence of UTF-16 code units. Most methods inspect text or return a new String; they do not edit the original object.

Methods

APIParametersReturnsBehaviorWatch for
length()None.int count of UTF-16 code units.Reports storage length, which matches simple character count for most beginner examples.Emoji and some non-BMP characters can count as two code units.
charAt(int index)Zero-based index from 0 to length() - 1.char at that code-unit index.Reads a single UTF-16 code unit without modifying the string.Throws StringIndexOutOfBoundsException for invalid indexes.
substring(int begin, int end)begin inclusive, end exclusive.New String containing the requested range.Copies the selected section into a separate String value.begin > end or indexes outside range throw StringIndexOutOfBoundsException.
indexOf(String target)Target substring to locate.First index or -1 if not found.Scans from left to right using literal matching.Check for -1 before using the result as an index.
split(String regex)Regular expression delimiter.String[] tokens.Breaks text around regex matches.Dots, pipes, and backslashes are regex metacharacters and often need escaping.
equals(Object other)Any object reference.boolean content equality.Returns true only when other is a String with the same characters.Use "literal".equals(value) when value might be null.

Which call fits

OptionBest forTradeoff
equals()Checking whether two strings have the same content.Case-sensitive and null-sensitive when called on a nullable variable.
equalsIgnoreCase()Simple case-insensitive comparisons such as yes/no input.Not a substitute for full locale-aware text rules.
==Reference identity checks.Usually wrong for string content comparisons.

Examples

Extract and validate parts

java
String email = "ada@example.com";
int at = email.indexOf("@");

if (at != -1 && email.endsWith(".com")) {
    String user = email.substring(0, at);
    System.out.println(user); // ada
}

Remember immutability

java
String name = " ada ";
name.trim();
System.out.println(name); // still has spaces

name = name.trim();
System.out.println(name); // ada

String name = " ada "; then name.trim(); then you print name. What is printed, and what would keep the trimmed text?

The print shows ada , spaces included. trim() returned a new string, and that return value was discarded. name = name.trim(); stores it, and the next print shows ada.