Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn String Manipulation Operations in Java | Strings
Introduction to Java

String Manipulation Operations in Java

Swipe to show menu

In real programs, strings are not static — they constantly get combined, searched, cleaned up, and transformed. This is called string manipulation: applying operations to change, inspect, or extract information from text.

String Concatenation

The simplest operation is concatenation — joining strings together using the + operator:

Main.java

Main.java

1234567891011
package com.example; public class Main { public static void main(String[] args) { String customerName = "Ann"; String welcomeMessage = "Welcome " + customerName; System.out.println(welcomeMessage); // Welcome Ann } }

All other string operations are based on methods — built-in actions Java provides. Instead of writing logic from scratch, we call these methods and let Java handle the operation. Think of them as ready-made tools applied directly to a string.

length()

If you need to know how many characters a string contains — including spaces — use the .length() method. It returns an int.

Main.java

Main.java

12345678910111213
package com.example; public class Main { public static void main(String[] args) { String customerName = "Ann"; String welcomeMessage = "Welcome " + customerName; int messageLength = welcomeMessage.length(); System.out.println(messageLength); } }

"Welcome Ann" has 11 characters including the space, so messageLength stores 11 and that's what gets printed.

toUpperCase()

If you need to convert all characters in a string to uppercase — for example on receipts or labels — use .toUpperCase(). It returns a new String.

Main.java

Main.java

123456789101112
package com.example; public class Main { public static void main(String[] args) { String customerName = "anna"; String upperCaseName = customerName.toUpperCase(); System.out.println(upperCaseName); } }

.toUpperCase() doesn't modify the original customerName — it creates a new String with all letters capitalized, so "anna" becomes "ANNA".

toLowerCase()

If you need all characters in lowercase — useful for consistency or comparison logic — use .toLowerCase(). It returns a new String.

Main.java

Main.java

123456789101112
package com.example; public class Main { public static void main(String[] args) { String order = "Large Latte with oat milk"; String lowerCaseOrder = order.toLowerCase(); System.out.println(lowerCaseOrder); } }

A new String is created with every character converted to lowercase, so the output is "large latte with oat milk".

contains()

If you need to check whether a string includes a specific word or piece of text, use .contains(). It returns a boolean.

Main.java

Main.java

123456789101112
package com.example; public class Main { public static void main(String[] args) { String order = "Large Latte with oat milk"; boolean containsLatte = order.contains("Latte"); System.out.println(containsLatte); } }

.contains() scans the entire string and checks if "Latte" appears anywhere inside it. Since it does, the result is true.

isEmpty()

If you need to check whether a string has any content at all, use .isEmpty(). It returns true if the string contains no characters, and false otherwise.

Main.java

Main.java

123456789101112
package com.example; public class Main { public static void main(String[] args) { String customerNote = ""; boolean emptyNote = customerNote.isEmpty(); System.out.println(emptyNote); } }

customerNote is an empty string with no characters, so .isEmpty() returns true.

replace()

If you need to swap out part of a string without rewriting it completely, use .replace(). It finds a specific piece of text and replaces it with something new. It returns a new String.

Main.java

Main.java

123456789101112
package com.example; public class Main { public static void main(String[] args) { String order = "Large Latte with oat milk"; String updatedMessage = order.replace("Latte", "coffee with milk"); System.out.println(updatedMessage); } }

.replace() finds every occurrence of "Latte" inside order and swaps it with "coffee with milk", producing "Large coffee with milk with oat milk".

charAt() and Indexing

If you need to access a single character at a specific position in a string, use .charAt(). It returns a char.

Main.java

Main.java

123456789101112
package com.example; public class Main { public static void main(String[] args) { String customerName = "Daniel"; char firstLetter = customerName.charAt(0); System.out.println(firstLetter); } }

.charAt(0) accesses position 0, which is the first character 'D', and stores it in the char variable firstLetter.

question mark

What does the .length() method return in Java?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

String Manipulation Operations in Java

In real programs, strings are not static — they constantly get combined, searched, cleaned up, and transformed. This is called string manipulation: applying operations to change, inspect, or extract information from text.

String Concatenation

The simplest operation is concatenation — joining strings together using the + operator:

Main.java

Main.java

1234567891011
package com.example; public class Main { public static void main(String[] args) { String customerName = "Ann"; String welcomeMessage = "Welcome " + customerName; System.out.println(welcomeMessage); // Welcome Ann } }

All other string operations are based on methods — built-in actions Java provides. Instead of writing logic from scratch, we call these methods and let Java handle the operation. Think of them as ready-made tools applied directly to a string.

length()

If you need to know how many characters a string contains — including spaces — use the .length() method. It returns an int.

Main.java

Main.java

12345678910111213
package com.example; public class Main { public static void main(String[] args) { String customerName = "Ann"; String welcomeMessage = "Welcome " + customerName; int messageLength = welcomeMessage.length(); System.out.println(messageLength); } }

"Welcome Ann" has 11 characters including the space, so messageLength stores 11 and that's what gets printed.

toUpperCase()

If you need to convert all characters in a string to uppercase — for example on receipts or labels — use .toUpperCase(). It returns a new String.

Main.java

Main.java

123456789101112
package com.example; public class Main { public static void main(String[] args) { String customerName = "anna"; String upperCaseName = customerName.toUpperCase(); System.out.println(upperCaseName); } }

.toUpperCase() doesn't modify the original customerName — it creates a new String with all letters capitalized, so "anna" becomes "ANNA".

toLowerCase()

If you need all characters in lowercase — useful for consistency or comparison logic — use .toLowerCase(). It returns a new String.

Main.java

Main.java

123456789101112
package com.example; public class Main { public static void main(String[] args) { String order = "Large Latte with oat milk"; String lowerCaseOrder = order.toLowerCase(); System.out.println(lowerCaseOrder); } }

A new String is created with every character converted to lowercase, so the output is "large latte with oat milk".

contains()

If you need to check whether a string includes a specific word or piece of text, use .contains(). It returns a boolean.

Main.java

Main.java

123456789101112
package com.example; public class Main { public static void main(String[] args) { String order = "Large Latte with oat milk"; boolean containsLatte = order.contains("Latte"); System.out.println(containsLatte); } }

.contains() scans the entire string and checks if "Latte" appears anywhere inside it. Since it does, the result is true.

isEmpty()

If you need to check whether a string has any content at all, use .isEmpty(). It returns true if the string contains no characters, and false otherwise.

Main.java

Main.java

123456789101112
package com.example; public class Main { public static void main(String[] args) { String customerNote = ""; boolean emptyNote = customerNote.isEmpty(); System.out.println(emptyNote); } }

customerNote is an empty string with no characters, so .isEmpty() returns true.

replace()

If you need to swap out part of a string without rewriting it completely, use .replace(). It finds a specific piece of text and replaces it with something new. It returns a new String.

Main.java

Main.java

123456789101112
package com.example; public class Main { public static void main(String[] args) { String order = "Large Latte with oat milk"; String updatedMessage = order.replace("Latte", "coffee with milk"); System.out.println(updatedMessage); } }

.replace() finds every occurrence of "Latte" inside order and swaps it with "coffee with milk", producing "Large coffee with milk with oat milk".

charAt() and Indexing

If you need to access a single character at a specific position in a string, use .charAt(). It returns a char.

Main.java

Main.java

123456789101112
package com.example; public class Main { public static void main(String[] args) { String customerName = "Daniel"; char firstLetter = customerName.charAt(0); System.out.println(firstLetter); } }

.charAt(0) accesses position 0, which is the first character 'D', and stores it in the char variable firstLetter.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
some-alt