codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

62 / 65 topics
61Java Keywords62Java String Methods63Java Math Methods64Java Arrays Methods65Java Collections Methods
Tutorials/Java Programming/Java String Methods
☕Java Programming

Java String Methods

Updated 2026-05-12
30 min read

Java String Methods

In the world of programming, strings are fundamental data types used extensively for storing and manipulating text. In Java, the String class provides a rich set of methods to perform various operations on string data. Understanding these methods is crucial for any developer working with Java, as they allow you to manipulate text efficiently and effectively.

In this tutorial, we will explore the most commonly used String methods in Java, providing examples and explanations for each. Whether you're a beginner or an experienced developer, this guide will equip you with the knowledge needed to handle strings more proficiently.

Core Content

1. Basic Methods

1.1 length()

The length() method returns the number of characters in a string.

LengthExample.java
1public class LengthExample {
2 public static void main(String[] args) {
3 String str = "Hello, World!";
4 int length = str.length();
5 System.out.println("Length of the string: " + length);
6 };
7}
Output
Length of the string: 13

1.2 charAt(int index)

The charAt(int index) method returns the character at the specified index in a string.

CharAtExample.java
1public class CharAtExample {
2 public static void main(String[] args) {
3 String str = "Hello, World!";
4 char ch = str.charAt(7);
5 System.out.println("Character at index 7: " + ch);
6 }
7}
Output
Character at index 7: W

2. Concatenation Methods

2.1 concat(String str)

The concat(String str) method concatenates the specified string to the end of this string.

ConcatExample.java
1public class ConcatExample {
2 public static void main(String[] args) {
3 String str1 = "Hello";
4 String str2 = " World!";
5 String result = str1.concat(str2);
6 System.out.println("Concatenated string: " + result);
7 }
8}
Output
Concatenated string: Hello World!

2.2 + Operator

The + operator can also be used to concatenate strings.

PlusOperatorExample.java
1public class PlusOperatorExample {
2 public static void main(String[] args) {
3 String str1 = "Hello";
4 String str2 = " World!";
5 String result = str1 + str2;
6 System.out.println("Concatenated string: " + result);
7 }
8}
Output
Concatenated string: Hello World!

3. Substring Methods

3.1 substring(int beginIndex)

The substring(int beginIndex) method returns a new string that is a substring of this string.

SubstringExample.java
1public class SubstringExample {
2 public static void main(String[] args) {
3 String str = "Hello, World!";
4 String subStr = str.substring(7);
5 System.out.println("Substring from index 7: " + subStr);
6 }
7}
Output
Substring from index 7: World!

3.2 substring(int beginIndex, int endIndex)

The substring(int beginIndex, int endIndex) method returns a new string that is a substring of this string.

SubstringRangeExample.java
1public class SubstringRangeExample {
2 public static void main(String[] args) {
3 String str = "Hello, World!";
4 String subStr = str.substring(0, 5);
5 System.out.println("Substring from index 0 to 5: " + subStr);
6 }
7}
Output
Substring from index 0 to 5: Hello

4. Comparison Methods

4.1 equals(Object anObject)

The equals(Object anObject) method compares this string to the specified object.

EqualsExample.java
1public class EqualsExample {
2 public static void main(String[] args) {
3 String str1 = "Hello";
4 String str2 = "Hello";
5 boolean isEqual = str1.equals(str2);
6 System.out.println("Are the strings equal? " + isEqual);
7 }
8}
Output
Are the strings equal? true

4.2 compareTo(String anotherString)

The compareTo(String anotherString) method compares two strings lexicographically.

CompareToExample.java
1public class CompareToExample {
2 public static void main(String[] args) {
3 String str1 = "Apple";
4 String str2 = "Banana";
5 int result = str1.compareTo(str2);
6 System.out.println("Comparison result: " + result);
7 }
8}
Output
Comparison result: -1

5. Case Conversion Methods

5.1 toUpperCase()

The toUpperCase() method converts all the characters in this string to uppercase.

ToUpperCaseExample.java
1public class ToUpperCaseExample {
2 public static void main(String[] args) {
3 String str = "Hello, World!";
4 String upperCaseStr = str.toUpperCase();
5 System.out.println("Uppercase string: " + upperCaseStr);
6 }
7}
Output
Uppercase string: HELLO, WORLD!

5.2 toLowerCase()

The toLowerCase() method converts all the characters in this string to lowercase.

ToLowerCaseExample.java
1public class ToLowerCaseExample {
2 public static void main(String[] args) {
3 String str = "Hello, World!";
4 String lowerCaseStr = str.toLowerCase();
5 System.out.println("Lowercase string: " + lowerCaseStr);
6 }
7}
Output
Lowercase string: hello, world!

6. Trimming Methods

6.1 trim()

The trim() method removes leading and trailing whitespace from this string.

TrimExample.java
1public class TrimExample {
2 public static void main(String[] args) {
3 String str = " Hello, World! ";
4 String trimmedStr = str.trim();
5 System.out.println("Trimmed string: '" + trimmedStr + "'");
6 }
7}
Output
Trimmed string: 'Hello, World!'

7. Replacement Methods

7.1 replace(char oldChar, char newChar)

The replace(char oldChar, char newChar) method returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

ReplaceExample.java
1public class ReplaceExample {
2 public static void main(String[] args) {
3 String str = "Hello, World!";
4 String replacedStr = str.replace('o', 'a');
5 System.out.println("Replaced string: " + replacedStr);
6 }
7}
Output
Replaced string: Hella, Warld!

7.2 replace(CharSequence target, CharSequence replacement)

The replace(CharSequence target, CharSequence replacement) method returns a new string resulting from replacing all occurrences of the literal target sequence in this string with the specified literal replacement.

ReplaceSequenceExample.java
1public class ReplaceSequenceExample {
2 public static void main(String[] args) {
3 String str = "Hello, World!";
4 String replacedStr = str.replace("World", "Java");
5 System.out.println("Replaced string: " + replacedStr);
6 }
7}
Output
Replaced string: Hello, Java!

8. Splitting Methods

8.1 split(String regex)

The split(String regex) method splits this string around matches of the given regular expression.

SplitExample.java
1public class SplitExample {
2 public static void main(String[] args) {
3 String str = "Hello,World,This,Is,Java";
4 String[] parts = str.split(",");
5 for (String part : parts) {
6 System.out.println(part);
7 }
8 }
9}
Output
Hello
World
This
Is
Java

Practical Example

Let's create a practical example that demonstrates the use of several string methods to process user input.

Java
1import java.util.Scanner;
2
3public class StringManipulationExample {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.println("Enter a sentence:");
7 String input = scanner.nextLine();
8
9 // Length of the string
10 int length = input.length();
11 System.out.println("Length of the input: " + length);
12
13 // Convert to uppercase and lowercase
14 String upperCaseInput = input.toUpperCase();
15 String lowerCaseInput = input.toLowerCase();
16 System.out.println("Uppercase: " + upperCaseInput);
17 System.out.println("Lowercase: " + lowerCaseInput);
18
19 // Trim whitespace
20 String trimmedInput = input.trim();
21 System.out.println("Trimmed Input: '" + trimmedInput + "'");
22
23 // Replace a character
24 String replacedInput = input.replace('a', 'e');
25 System.out.println("Replaced 'a' with 'e': " + replacedInput);
26
27 // Split the string into words
28 String[] words = input.split("s+");
29 System.out.println("Words in the sentence:");
30 for (String word : words) {
31 System.out.println(word);
32 }
33
34 scanner.close();
35 }
36}

Summary

MethodDescription
length()Returns the length of the string.
charAt(int index)Returns the character at the specified index.
concat(String str)Concatenates the specified string to this string.
substring(int beginIndex)Returns a substring from the specified index.
equals(Object anObject)Compares this string with the specified object.
compareTo(String anotherString)Compares two strings lexicographically.
toUpperCase()Converts all characters to uppercase.
toLowerCase()Converts all characters to lowercase.
trim()Removes leading and trailing whitespace.
replace(char oldChar, char newChar)Replaces occurrences of a character.
split(String regex)Splits the string around matches of a regular expression.

What's Next?

In the next tutorial, we will explore Java Math Methods, which provide various mathematical operations and functions. These methods are essential for performing calculations in your Java programs. Stay tuned!


PreviousJava KeywordsNext Java Math Methods

Recommended Gear

Java KeywordsJava Math Methods