String Methods Java

String Methods Java

While we are working with the String class, there are some methods that we can use to do different things with the Strings, like finding the length of a String, converting the string from lowercase to uppercase, and finding some character at some index.

Now if you are wondering that – hey!!! you had told me that strings are immutable in java, which means that once created, you cannot change them. So, how can we convert it into lowercase or uppercase?

Well, let me tell you that it is right that Strings are immutable in java, but as we have already seen, we are creating another copy of that string with effective changes in the string. Like if you create a string, and after that, try converting it to uppercase, it will create a copy of the string, in uppercase. Have a look at the below program, which demonstrates this thing. I have made use of the toLowerCase() method, which is one of the methods we are going to discuss ahead.

public class Stringexample {
public static void main(String[] args) {
String exampleString = new String(“OOPS! I LEFT MY CAPS LOCK ON…”);
System.out.println(exampleString.toLowerCase());
System.out.println(exampleString);
}
}

In the above example, we have printed both the strings, the one that we converted into lowercase, and the one that we had created. You can observe a very important thing that the original string that we had created, is as it is. When we applied the method toLowerCase on the string object, we just made a new object, with the new value. This is not having any reference. While we converted it to lowercase, a new object was created with the lowercase string.

So, the toLowerCase() is one of the many methods that we are going to see here. You might be wondering how are we calling the method? Means why did I do exampleString.toLowerCase()?

Let’s explain this –

This relates to the concept of object-oriented programming, but let’s see it here in brief since we are going to repeatedly use it here. We know that the String here, is a class, and we are creating a String object. With class, if you are not familiar, there are some behaviors (methods / a.k.a functions in other languages) and some attributes (class-specific variables). Whenever we create an object, it can access some attributes and behaviors from the class. To access the methods or attributes for that particular object, we use such syntax, which we have used above. We just do objectname.methodname().

So, in case you get confused or not understand what do we mean by this, just follow along what we are doing. With time, you will get used to this thing, and also understand what are we doing.

Below are some methods of the String class –

MethodDetails
length()This method, as the name says, is used to get the length of the string. In other words, we use this method to get the number of characters in the string.
toLowerCase()Using this method, we get the lowercase version of the String. Remember that a separate object is created
toUpperCase()Using this method, we get the uppercase version of the string.
charAt(int index)Using this method, we can get a character at a particular index. Like if we have a string ”hello”, and we try this method with index 3, we get the character ‘l’. Remember that if we give such an index, like 30, which is above the length of the string, we get an error. We are going to see this in an example ahead.
concat(String str)Using this method, we can contact one string at the end of another string. This is similar to how we concatenate with the + operator. So, we can use them alternatively, but there is a method as well for doing this, that’s what I wanted to mention. We will have an example of this ahead.
equals(String str)This method compares two strings based on the content of the string. We are going to have some examples for this method as well.
equalsIgnoreCase(String anotherString)This method is similar to the above one, just here, we are ignoring the case here. If we wish to ignore the case of the string, then we can use this method.
ends with(String suffix)With this method, we check if the given string ends with some given suffix. So, for example, if we have a string – ”Hello”, and we check whether or not the string ends with ”lo”… this will give an output as true. So one more thing that we need to keep in mind is that this method just says yes or no. (true or false).
isEmpty()This method checks whether or not the string is empty. If the string is empty, we get the output as true. Else we get the output as false. This is like asking – hey… is this string empty?

 

We are going to have an example for this method too.

indexOf(String str)With this method, we get the first occurrence index of
the given string. We are going to have an example of
this method. For example, we have a string – ”Welcome
to GyaniPandit”, and we are trying to find an index of
let’s say – ”GyaniPandit” here, we get the position where
the string starts.
indexOf(char ch)With this method, we can find the first occurrence index of some character in the string. We are going to have an example of this method ahead.
lastIndexOf(char ch)As the name says, this is going to give the last index of some character in the string.
lastIndexOf(String str)As the name says, this method is going to give the last index of some string in the string. We are going to see examples for both methods.
replace(char oldchar, char newchar)This method, as the name says, replaces the specified old character, with the new character in some given string. If the string is – ”jumble”, and you call this method with the old character as ‘j’ to be replaced by ‘r’, then your new string will be ”rumble”.
replaceAll(String regex, String replacement)With this method, we can replace a specified string with some string replacement. We are going to have an example of this.
startsWith(String prefix)With this method, we can check whether or not the string starts with the specified prefix (string).
trim()With this method, we can cut the spaces from the beginning and the end of the string, and just return the string. We are going to have an example related to this method for a better understanding.
strip()With this method, we can cut the whitespaces from the beginning and the end of the string.
stripLeading()This method returns a string removing the whitespaces from the beginning of the string(leading whitespaces).
stripTrailing()This method returns a string removing the whitespaces from the ending of the string(trailing whitespaces).
contains(CharSequence sequence)This method lets us find out whether a string contains some sequence of characters. We are going to solve some examples for this method ahead.
split(String regex)This method splits the string on the given regular expression, and it returns an array of strings split over the regular expression. This is like if you want to have the string split over spaces, you can get it with this method, and then get all the words of the string into a character array.
substring(int beginIndex)With this method, we can get a substring from the specified string. It takes the beginning index as an argument and returns a string from the beginning index to the end, as a substring.
substring(int beginIndex, int endIndex)With this method, we can get a substring from the specified string, but this time it is taking 2 arguments, which is the beginning index and the ending index. This is going to return a string from the specified beginning index to the end index from the original string. Just remember that the end index is not included. We get the string up to end – 1.