18
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.
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());
}
}
You can run your code online here
![](https://res.cloudinary.com/practicaldev/image/fetch/s--M9ybJMEe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/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.
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
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());
}
}
You can run your code online here
![](https://res.cloudinary.com/practicaldev/image/fetch/s--iX3abHTR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/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.
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);
}
}
You can run your code online here
![](https://res.cloudinary.com/practicaldev/image/fetch/s--HM1wqtvV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/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.
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
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"));
}
}
You can run your code online here
There are several other methods to compare in Java, I'll write about those in a separate article.
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);
}
}
You can run your code online here
![](https://res.cloudinary.com/practicaldev/image/fetch/s--h58e3A9Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1626684551520/fmxN0yLo3.png)
Cover:- Rajat Gour
My personal blog blog.ritvikdubey.com
18