
Java is a programming language that has been widely used in the software industry for a long time, and it remains popular due to the security it offers for applications built with it.
Text is one of the most common types of data used in software applications. Usernames, email addresses, product names, search queries, messages, and addresses are all examples of text data. In Java, text is represented using the String class.
Strings are used in almost every Java application. Whether you are building a website, a mobile application, or a backend service, you will frequently work with text data. Java provides many built-in methods that make it easy to manipulate, search, compare, and transform strings.
In this chapter, you will learn how to create strings, access individual characters, and use the most common String methods available in Java.
A String is a sequence of characters.
Examples
"Hello" "Java" "OpenAI" "john@example.com"
Unlike primitive data types such as int and double, a String is an object in Java.
Syntax
String variableName = "value";
Example
String name = "Rahul";
Here:
String is the data typename is the variable name"Rahul" is the valueExample
String company = "OpenAI"; System.out.println(company);
Output
OpenAI
The length() method returns the number of characters in a string.
Example
String language = "Java"; System.out.println(language.length());
Output
4
Explanation
The word Java contains four characters.
The toUpperCase() method converts all characters to uppercase.
Example
String language = "java"; System.out.println(language.toUpperCase());
Output
JAVA
The toLowerCase() method converts all characters to lowercase.
Example
String language = "JAVA"; System.out.println(language.toLowerCase());
Output
java
The charAt() method returns the character at a specified index.
Example
String language = "Java"; System.out.println(language.charAt(0));
Output
J
Explanation
Indexes start from 0.
J a v a 0 1 2 3
The indexOf() method returns the position of a character or substring.
Example
String language = "Java";
System.out.println(language.indexOf('v'));
Output
2
The contains() method checks whether a string contains specific text.
Example
String email = "john@example.com";
System.out.println(email.contains("@"));
Output
true
To compare strings, use the equals() method.
Example
String password1 = "admin123"; String password2 = "admin123"; System.out.println(password1.equals(password2));
Output
true
Explanation
The values are identical, so the result is true.
The equalsIgnoreCase() method ignores uppercase and lowercase differences.
Example
String city1 = "Mumbai"; String city2 = "mumbai"; System.out.println(city1.equalsIgnoreCase(city2));
Output
true
The substring() method extracts a portion of a string.
Example
String email = "john@example.com"; System.out.println(email.substring(0, 4));
Output
john
The replace() method replaces characters or text.
Example
String text = "Java Programming";
System.out.println(text.replace("Java", "Python"));
Output
Python Programming
The trim() method removes spaces from the beginning and end of a string.
Example
String username = " Rahul "; System.out.println(username.trim());
Output
Rahul
Example
String website = "https://example.com";
System.out.println(website.startsWith("https"));
Output
true
Example
String file = "photo.jpg";
System.out.println(file.endsWith(".jpg"));
Output
true
The split() method breaks a string into multiple parts.
Example
String tags = "Java,Python,JavaScript";
String[] result = tags.split(",");
System.out.println(result[0]);
System.out.println(result[1]);
System.out.println(result[2]);
Output
Java Python JavaScript
Strings in Java are immutable.
This means that once a String is created, its value cannot be changed.
Example
String name = "Rahul"; name.toUpperCase(); System.out.println(name);
Output
Rahul
The original string remains unchanged.
Correct
name = name.toUpperCase(); System.out.println(name);
Output
RAHUL
Example
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your email: ");
String email = sc.nextLine();
if(email.contains("@")) {
System.out.println("Valid Email Format");
}
else {
System.out.println("Invalid Email Format");
}
}
}
Input
john@example.com
Output
Valid Email Format
Explanation
@.== for String ComparisonWrong
String a = "Java"; String b = "Java"; System.out.println(a == b);
Correct
System.out.println(a.equals(b));
Always use equals() when comparing string values.
Wrong
String text = "Java"; System.out.println(text.charAt(4));
Problem
Valid indexes are:
0 1 2 3
Index 4 does not exist.
Correct
System.out.println(text.charAt(3));
length() returns the number of characters.charAt() accesses individual characters.equals() compares string values.substring() extracts text.replace() modifies content.