DEV Community

Cover image for String Methods
Ritvik Dubey
Ritvik Dubey

Posted on

String Methods

Hello all๐Ÿ˜€ I hope you are doing well. In this article I'll be writing about some most commonly used methods of string class in java. May be I'll write one more article about the methods. In this article I'll go in some depth to make it easy to understand how to use String class methods. If you haven't read my last article then please go through that short read first it about String class.

Let's begin...

1. length()

This method returns the length of a specified string. This method returns count of total number of characters. This method returns integer value. This method is most commonly used while you are working on loops.

public class Test {
    public static void main(String []args)
    {
        String str = "This is a test string";
        System.out.println("The length of the String is :  " + str.length());
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-length()-method.png

There is this one length method also there in java. This method is used to find out the length of an array in java.

public class DemoArray {
    public static void main(String []args)
    {
        int[] arr = new int[69];
        System.out.println("The length of the array is : " + arr.length);
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-array-length-method.png

2. isEmpty()

This method checks if a given string is empty or not. This method returns True if the string is empty which means if the length of the given string is 0. This method returns boolean value.

public class Demo {  
    public static void main(String []args) {  
        String str1="";  
        String str2="This is a test string";  
        System.out.println("This is for str1 : " + str1.isEmpty());  
        System.out.println("This is for str2 : " + str2.isEmpty());  
    }
} 
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-isEmpty()-method.png

There is this method isBlank(), this method is similar to isEmpty() but additionally checks for whitespace in the given String. This method was introduced in Java 11.

3. toUpperCase()

This method returns the UPPERCASE from of a specified string. This method converts all of the characters in this String to upper case. Since Java is a case-sensitive programming language this method have its own importance. This method is mostly used when dealing with validations.

public class Demo {
    public static void main(String []args) {
        String str = "this complete string was in lower case";
        String subStr = str.toUpperCase();
        System.out.println(subStr);
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-toUpperCase()-method.png

There is this method toLowerCase() which is used to convert the given string into lower-case.

public class Demo {
    public static void main(String []args) {
        String str = "THIS COMPLETE STRING WAS IN UPPER CASE";
        String subStr = str.toLowerCase();
        System.out.println(subStr);
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here
java-toLowerCase()-method.png

4. equals()

This method returns true if and only if the object is a string and represents the same sequence of characters as this string. Returns true if the current object is the equal as the argument and returns false if objects are not equal.

public class Demo {
    public static void main(String []args) {
        String str = "First string";
        System.out.println(str.equals("First string"));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here
java-equals()-method.png

There are several other methods to compare in Java, I'll write about those in a separate article.

5. charAt()

This method returns the character at the given index in the specified string. The indexing starts from 0 here, i.e., the first letter is at 0 index. This method returns a char value.

public class Demo {
    public static void main(String []args) {
        String str = "To find index of character at 15th index in the string";
    char result = str.charAt(15);
        System.out.println("Character at 15th index is : " + result);
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-charAt()-method.png

Okay so that's enough for now follow my this journey to learn more about Java.

Thank you for reading.

Please share your thoughts about it and correct me if I'm wrong.

I hope you liked it and found it helpful.

Cover:- Rajat Gour

Connect with me on Twitter or LinkedIn

My personal blog blog.ritvikdubey.com

Top comments (0)