Java

Java String Methods

Java provides a rich set of built-in methods for handling String operations efficiently. Since strings are immutable in Java, these Java String Methods help in manipulation, searching, formatting, and comparing strings without altering the original value. This article will explore some of the most commonly used Java string methods with examples.

1. Understanding Strings in Java

In Java, a String is an object that represents a sequence of characters. It is defined under the java.lang package and is widely used for text-based operations.

Declaring a String

String str1 = "Hello, World!";
String str2 = new String("Java Programming");

2. Java String Methods with Examples

This method returns the number of characters in a string, including spaces and special symbols.

public class Main {
    public static void main(String[] args) {
        String text = "Java Programming";
        System.out.println("Length: " + text.length()); // Output: 16
    }
}

2.2 Convert to Upper and Lower Case (toUpperCase(), toLowerCase())

These methods convert a string to uppercase or lowercase.

public class Main {
    public static void main(String[] args) {
        String name = "Java";
        System.out.println(name.toUpperCase()); // Output: JAVA
        System.out.println(name.toLowerCase()); // Output: java
    }
}

2.3 String Comparison (equals(), equalsIgnoreCase())

The equals() method checks whether two strings are exactly the same, while equalsIgnoreCase() ignores case differences.

public class Main {
    public static void main(String[] args) {
        String str1 = "Java";
        String str2 = "JAVA";

        System.out.println(str1.equals(str2));           // Output: false
        System.out.println(str1.equalsIgnoreCase(str2)); // Output: true
    }
}

2.4 Checking if a String Contains a Substring (contains())

This method checks if a string contains a specific sequence of characters.

public class Main {
    public static void main(String[] args) {
        String sentence = "Welcome to Java Programming";
        System.out.println(sentence.contains("Java")); // Output: true
    }
}

2.5 Extracting a Substring (substring())

The substring() method is used to extract a portion of a string.

public class Main {
    public static void main(String[] args) {
        String text = "Hello Java";
        System.out.println(text.substring(6));    // Output: Java
        System.out.println(text.substring(0, 5)); // Output: Hello
    }
}

 

admin

Recent Posts

ChatGPT Free Online Download the ChatGPT App Easily

Have you ever wanted to talk to a friendly, smart robot that helps you with…

4 days ago

Free GPT Chat? DeepSeek AI Does It Better

DeepSeek AI is becoming a friendly and powerful new tool in the world of artificial…

2 weeks ago

Spring Boot MySQL Complete CRUD REST API [ Free Sourecode ]

Do you want to become an expert in Spring Boot CRUD operations? This comprehensive tutorial…

3 weeks ago

CREATE Responsive Navigation Bar with FlexBox CSS!

Modern websites must have a navigation bar that is clear and responsive. FlexBox CSS is…

4 weeks ago

Registration with image upload Java Jdbc(Download Source code)

Introduction In this section, we will guide you step by step in the development of an image upload registration system in Java using MySQL and JDBC. In the application, users register…

2 months ago

Touchable shop Pos system using Java

The Touchable Shop POS (Point of Sale) system is a sophisticated software solution developed using…

3 months ago