Skip to content
Justice Tech School

Java String Methods

Strings are immutable objects. Useful methods: length, charAt, substring, indexOf, contains, replace, split, trim, strip, toUpperCase, equals, equalsIgnoreCase, compareTo, isEmpty and join. Use equals, never == for text.

Example
String s = "Hello, Java";
System.out.println(s.length() + " " + s.charAt(1) + " " + s.substring(7));
System.out.println(s.indexOf("Java") + " " + s.replace("Java", "World"));
System.out.println(String.join("-", "a,b,c".split(",")));
System.out.println("ABC".equalsIgnoreCase("abc") + " " + "b".compareTo("a"));

Result

11 e Java
7 Hello, World
a-b-c
true 1

Exercise

Compare text content.

s.("x")

Quiz Test

Which compares string contents?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.