CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > String > Java String Methods: Essential Operations You Must Know

Java String Tutorial

  • Introduction to String
  • String Immutability
  • Common Methods
  • String Comparison
  • Formatting in String
  • Performance Optimization
  • String Manipulation
  • Java Regex Example
  • Special Characters in String
  • String Security
  • Java 8+ String Enhancements
  • Internationalization
  • Advanced Java String Techniques
  • Java String Best Practices

Java String Methods: Essential Operations You Must Know

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

Table of Contents

  • Core String Methods for Daily Use
    • 1. Getting Basic Information
    • 2. Extracting Substrings
    • Common Mistake:
    • 3. Searching Strings
    • 4. Concatenation: Combining Strings
    • 5. Case Conversion
    • 6. Trimming Whitespace
  • Best Practices
  • FAQ
    • What’s the difference between trim() and strip()?
    • Why is concat() rarely used?

Core String Methods for Daily Use

Java’s String class provides dozens of methods for text processing. Let’s break down the most critical ones.

1. Getting Basic Information

  • length(): Returns the number of characters.
String text = "Java";  
System.out.println(text.length());  // 4  
  • charAt(int index): Fetch a character at a specific position.
System.out.println("Hello".charAt(1));  // 'e' (indices start at 0)  

2. Extracting Substrings

  • substring(int beginIndex): Get text from beginIndex to end.
String s = "Programming";  
System.out.println(s.substring(3));  // "gramming"  
  • substring(int beginIndex, int endIndex): Extract between indices (exclusive of endIndex).
System.out.println(s.substring(0, 4));  // "Prog"  

Common Mistake:

// Causes StringIndexOutOfBoundsException if indices are invalid  
String s = "Hi";  
s.substring(0, 5);  // ❌ Avoid!  

3. Searching Strings

  • indexOf(String str): Find the first occurrence’s position.
String log = "Error: File not found";  
System.out.println(log.indexOf("Error"));  // 0  
System.out.println(log.indexOf("Warn"));   // -1 (not found)  
  • contains(CharSequence s): Check if a substring exists.
System.out.println(log.contains("not"));  // true  

4. Concatenation: Combining Strings

  • Using + Operator:
String name = "Alice";  
String greeting = "Hello, " + name + "!";  // "Hello, Alice!"  
  • concat(String str):
String s1 = "Java";  
String s2 = s1.concat("Script");  // "JavaScript"  

Key Difference:

  • + handles null gracefully ("null" string).
  • concat() throws NullPointerException if argument is null.

5. Case Conversion

  • toUpperCase() / toLowerCase():
String lang = "Java";  
System.out.println(lang.toUpperCase());  // "JAVA"  
System.out.println("HELLO".toLowerCase());  // "hello"  

Pitfall: Forgetting to assign the result (Strings are immutable!):

String s = "text";  
s.toUpperCase();  // ❌ Original 's' remains "text"  
s = s.toUpperCase();  // ✅ Correct: s becomes "TEXT"  

6. Trimming Whitespace

  • trim(): Remove leading/trailing spaces (old method).
String input = "   Java  ";  
System.out.println(input.trim());  // "Java"  
  • strip() (Java 11+): Handles Unicode whitespace (preferred).
String unicodeSpace = "\u2000Hello\u2000";  
System.out.println(unicodeSpace.strip());  // "Hello"  

Best Practices

  1. Prefer isEmpty() Over length() == 0:
if (str.isEmpty()) { ... }  // Cleaner than checking length  
  1. Use strip() for Modern Whitespace Handling (Java 11+).
  2. Avoid Chaining substring() Calls: Creates unnecessary intermediate strings.

FAQ

What’s the difference between trim() and strip()?

trim() removes only ASCII spaces (<= U+0020), while strip() handles all Unicode whitespace (e.g., \u2000).

Why is concat() rarely used?

+ is more readable and flexible (e.g., "A" + null vs. "A".concat(null)).

Up Next: String Comparison: Learn to compare Strings safely with equals(), compareTo(), and more.

Related Posts:

  • Control Statements in Java
  • Java String Best Practices and Common Pitfalls
  • Java 8+ String Enhancements: New Methods and Best Practices
  • java.lang.Character Class in Java
  • Concat Column in MySQL
  • Java String Manipulation: Replace, Split, and Join
Tags:javalanguage-fundamentalsString
Was this article helpful?
← Previous ArticleJava String Immutability Explained: Why Strings Can’t Change
Next Article →Java String Comparison: equals(), ==, and compareTo() Explained

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap