Java provides many built-in String methods to work with text. These methods help us find the length of a String, change its case, search for text, extract characters, replace text, and compare String values.
String methods are predefined methods of the Java
String class. They are called using a
String variable followed by a dot and the method name.
public class Main {
public static void main(String[] args) {
String name = "Rahul";
System.out.println(name.length());
System.out.println(name.toUpperCase());
System.out.println(name.toLowerCase());
}
}
5
RAHUL
rahul
The methods return information or a result based on the String on which they are called.
The length() method returns the number of
UTF-16 code units in a String. For basic English text,
this normally matches the number of characters.
public class Main {
public static void main(String[] args) {
String name = "Amit";
String city = "New Delhi";
System.out.println(name.length());
System.out.println(city.length());
}
}
4
9
Spaces are included when calculating String length.
These methods return a String converted to uppercase or lowercase.
public class Main {
public static void main(String[] args) {
String text = "Java Programming";
System.out.println(text.toUpperCase());
System.out.println(text.toLowerCase());
}
}
JAVA PROGRAMMING
java programming
These methods do not modify the original String. Java Strings are immutable.
Use equals() to compare String contents.
Use equalsIgnoreCase() when letter case
should not affect the comparison.
public class Main {
public static void main(String[] args) {
String a = "Java";
String b = "Java";
String c = "java";
System.out.println(a.equals(b));
System.out.println(a.equals(c));
System.out.println(a.equalsIgnoreCase(c));
}
}
true
false
true
== checks whether two references
refer to the same object, while equals()
compares String contents.
The charAt() method returns the character
at a specified index. String indexing starts at zero.
public class Main {
public static void main(String[] args) {
String word = "JAVA";
System.out.println(word.charAt(0));
System.out.println(word.charAt(1));
System.out.println(word.charAt(3));
}
}
J
A
A
| Index | Character |
|---|---|
| 0 | J |
| 1 | A |
| 2 | V |
| 3 | A |
An invalid index causes a
StringIndexOutOfBoundsException.
The contains() method checks whether a
String contains a specified sequence of characters.
It returns a Boolean value.
public class Main {
public static void main(String[] args) {
String message = "I am learning Java";
System.out.println(message.contains("Java"));
System.out.println(message.contains("Python"));
System.out.println(message.contains("learning"));
}
}
true
false
true
The search is case-sensitive. For example,
"Java" and "java" are
different sequences.
indexOf() finds the first occurrence of
a character or substring. lastIndexOf()
finds its last occurrence.
public class Main {
public static void main(String[] args) {
String text = "Java Programming Java";
System.out.println(text.indexOf("Java"));
System.out.println(text.lastIndexOf("Java"));
System.out.println(text.indexOf("Python"));
}
}
0
17
-1
If the searched text is not found,
indexOf() and lastIndexOf()
return -1.
The substring() method extracts part of
a String.
public class Main {
public static void main(String[] args) {
String text = "Programming";
System.out.println(text.substring(0, 7));
System.out.println(text.substring(3));
}
}
Program
gramming
In substring(start, end), the start index
is included and the end index is excluded.
"Programming".substring(0, 3) returns
"Pro".
The replace() method returns a new String
with matching characters or character sequences
replaced by another value.
public class Main {
public static void main(String[] args) {
String text = "I like Python";
String newText = text.replace("Python", "Java");
System.out.println(text);
System.out.println(newText);
}
}
I like Python
I like Java
The original String remains unchanged. The returned String contains the replacement.
These methods remove whitespace from the beginning and end of a String.
public class Main {
public static void main(String[] args) {
String name = " Rahul Kumar ";
System.out.println(name.trim());
System.out.println(name.strip());
}
}
Rahul Kumar
Rahul Kumar
trim() removes leading and trailing
characters up to U+0020. strip() uses
Unicode whitespace rules and is available in Java 11
and later.
The startsWith() method checks the
beginning of a String. The endsWith()
method checks its ending.
public class Main {
public static void main(String[] args) {
String file = "java_program.java";
System.out.println(file.startsWith("java"));
System.out.println(file.endsWith(".java"));
System.out.println(file.endsWith(".php"));
}
}
true
true
false
These methods are useful when checking file extensions, prefixes, or specific text patterns.
These methods help check String content and combine text.
public class Main {
public static void main(String[] args) {
String a = "";
String b = " ";
String c = "Hello ";
System.out.println(a.isEmpty());
System.out.println(b.isEmpty());
System.out.println(b.isBlank());
System.out.println(c.concat("Java"));
}
}
true
false
true
Hello Java
isEmpty() checks whether the String length is zero.isBlank() checks whether the String is empty or contains only whitespace. It requires Java 11 or later.concat() joins another String to the current String.This program demonstrates several String methods in one practical example.
public class Main {
public static void main(String[] args) {
String firstName = " Rahul";
String lastName = "Kumar ";
firstName = firstName.strip();
lastName = lastName.strip();
String fullName = firstName.concat(" ").concat(lastName);
System.out.println("Full Name: " + fullName);
System.out.println("Name Length: " + fullName.length());
System.out.println("Uppercase: " + fullName.toUpperCase());
System.out.println("Lowercase: " + fullName.toLowerCase());
System.out.println("Starts with Rahul: " +
fullName.startsWith("Rahul"));
System.out.println("Contains Kumar: " +
fullName.contains("Kumar"));
}
}
Full Name: Rahul Kumar
Name Length: 11
Uppercase: RAHUL KUMAR
Lowercase: rahul kumar
Starts with Rahul: true
Contains Kumar: true
length() counts UTF-16 code units.toUpperCase() and toLowerCase() change letter case in the returned String.equals() compares String contents.charAt() retrieves a character at an index.contains() searches for text.indexOf() and lastIndexOf() find positions.substring() extracts part of a String.replace() returns a String with replacements.trim() and strip() remove leading and trailing whitespace according to their respective rules.Q1. Which method returns the length of a String?
Q2. Which method compares String contents?
Q3. What does "Java".toUpperCase() return?