Categories: Java

Some examples of string class methods

The methods listed below are some of the most frequently used String class methods in Java.

charAt() method:

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

class HelloWorld {
    public static void main(String[] args) {
        String myStr = "Hello";
        char result = myStr.charAt(0);
        System.out.println(result);
    }
}
Output:
H
equalsIgnoreCase() method:

equalsIgnoreCase() checks for equality between two Strings while ignoring their case.

        class HelloWorld {
    public static void main(String[] args) {
        String myStr = "Hello",str="Coder";
        boolean result = myStr.equalsIgnoreCase(str);
        System.out.println(result);
    }
}
Output:
false
indexOf() method:

The index of the first occurrence of a substring or a character is returned by the indexOf() method.

 class HelloWorld {
    public static void main(String[] args) {
       String myStr = "Hello planet earth, you are a great planet.";
    System.out.println(myStr.indexOf("e", 5));
    }
}
Output:
10
length() method:

The length() function of a String returns the number of characters in it.

 class HelloWorld {
    public static void main(String[] args) {
       String myStr = "Hello planet earth, you are a great planet.";
    System.out.println(myStr.length());
    }
}
Output:
43
replace() method:

The replace() method in a string replaces all occurrences of a character with a new character.

public class Demo {
    public static void main(String[] args) {   
        String str = "coderzpy";
        System.out.println(str.replace('c','C'));
    }
}
Output:
Coderzpy
substring() method:

The substring() method returns a part of the string.

public class Demo {
    public static void main(String[] args) {
        String str = "ABCDEFGHIJKLMNO";
        System.out.println(str.substring(4));
        System.out.println(str.substring(4,7));        
    }
}
Output:
EFGHIJKLMNO
EFG
valueOf() method:

String class uses an overloaded version of valueOf() method for all primitive data types and for type Object.

public class Demo {
    public static void main(String[] args) {
         int num = 115;
         String s1 = String.valueOf(num);    //converting int to String
         s1+= "hello";
         System.out.println(s1);
         System.out.println("type of num : "+s1.getClass().getName());   
    }
}
Output:
115hello
type of num : java.lang.String
trim() method:

This method returns a string that has been stripped of any leading and trailing whitespaces.

public class Demo {
    public static void main(String[] args) {
        String str = "   hello coderzpy  ";
        System.out.println(str.trim());   
    }
}
Output:
hello coderzpy
endsWith() method:

the endsWith() method is used to determine whether the string ends with the specified suffix. If the suffix matches the string, it returns true; otherwise, it returns false.

public class Demo {
    public static void main(String[] args) {
        String a="Hello welcome to Coderzpy.com";  
        System.out.println(a.endsWith("m"));  
        System.out.println(a.endsWith("com"));       
    }
}
    
Output:
true
true

Note: also read about the String class methods in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago