String Methods
Tags: java, beginners, discuss, javascript
Published: 2021-07-18 18:21:12

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](https://dev.to/ritvikdubey27/string-class-in-java-4jb7)***.
## 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*. 

```Java
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());
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-length()-method.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626670063796/3el7o2rif.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.
```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);
    }
}
``` 
>***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

>![java-array-length-method.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626670504053/KsCI8-qqj.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.

```java
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());  
    }
} 
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-isEmpty()-method.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626672025603/a-Y_ZZ2N4.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.

```Java
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);
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-toUpperCase()-method.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626682366330/XGm9nK2nu.png)
> There is this method toLowerCase() which is used to convert the given string into lower-case.
```Java
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);
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***
![java-toLowerCase()-method.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626682614017/5_qD_srcy.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. 

```Java
public class Demo {
    public static void main(String []args) {
        String str = "First string";
        System.out.println(str.equals("First string"));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***
![java-equals()-method.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626683365476/OYn48K3dx.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. 

```java
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);
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-charAt()-method.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626684551520/fmxN0yLo3.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](https://twitter.com/rajatgour20)***
#### *Connect with me on [Twitter](https://twitter.com/ritvikdubey27?s=09) or [LinkedIn](https://www.linkedin.com/in/ritvik-dubey-3bbb22169)*
#### *My personal blog [blog.ritvikdubey.com](https://blog.ritvikdubey.com/)*