HomeCoding and ProgrammingWrite a Java program to practice using String class and its methods

Write a Java program to practice using String class and its methods

Java program that practices using the String class and its methods:

public class StringPractice {
    public static void main(String[] args) {
        String str1 = "Hello, world!";
        String str2 = "hello, world!";
        String str3 = "Hello, Java!";
        String str4 = "   Java   ";
        
        // String length method
        System.out.println("Length of str1: " + str1.length());

        // String charAt method
        System.out.println("First character of str1: " + str1.charAt(0));

        // String substring method
        System.out.println("Substring of str1: " + str1.substring(0, 5));

        // String equals method
        System.out.println("Are str1 and str2 equal? " + str1.equals(str2));

        // String equalsIgnoreCase method
        System.out.println("Are str1 and str2 equal (ignore case)? " + str1.equalsIgnoreCase(str2));

        // String compareTo method
        System.out.println("Comparing str1 and str3: " + str1.compareTo(str3));

        // String indexOf method
        System.out.println("Index of 'world' in str1: " + str1.indexOf("world"));

        // String replace method
        System.out.println("Replacing ',' with '!' in str1: " + str1.replace(',', '!'));

        // String trim method
        System.out.println("Trimming whitespace from str4: " + str4.trim());
    }
}

In this program, we define a StringPractice class with a main method that creates several String objects and uses various String methods to manipulate them. The methods we use include:

  • length: returns the length of a string.
  • charAt: returns the character at a specific index in a string.
  • substring: returns a substring of a string.
  • equals: compares two strings for equality.
  • equalsIgnoreCase: compares two strings for equality, ignoring case.
  • compareTo: compares two strings lexicographically.
  • indexOf: returns the index of a substring within a string.
  • replace: replaces characters within a string.
  • trim: removes whitespace from the beginning and end of a string.

When we run the program, we see the output:

Length of str1: 13
First character of str1: H
Substring of str1: Hello
Are str1 and str2 equal? false
Are str1 and str2 equal (ignore case)? true
Comparing str1 and str3: -8
Index of 'world' in str1: 7
Replacing ',' with '!' in str1: Hello! world!
Trimming whitespace from str4: Java

This demonstrates the use of various String methods in Java. We can use these methods to manipulate and compare strings, extract substrings, replace characters, and remove whitespace.

Naman Raj
Naman Rajhttps://www.digitalnamanji.com/
Hello friends, I am Naman, Technical Author & Founder of Digitalnamanji. I am a Computer Science Engineer and I do coding and programming. At the same time, I am interested in knowing various types of information related to internet and technology and we also teach others.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular