Methods of the String object in Java

In the previous tutorial, you learned some ways to initialize a String object in Java as well as the differences between them, and you also learned how String implements the Immutable concept. In this tutorial, I will continue to present to you all the basic methods of the String object.


charAt() method

Used to get a character at the index in the String.

Remember that String is stored in an array variable, so the index will start at zero.

For example:

Result:

Methods of the String object in Java

In this example, the character at index = 0 will be “K”, 1 will be “h”, and so at index = 2 will be “a”.


indexOf() method

Used to search for a character or a string in the String.

This method returns the index that starts the string or character.

This method has many different overload methods, which help us to search for a character, a string, a character starting with a certain index, or a string starting with a certain index.

For example:

Result:

Methods of the String object in Java


substring() method

This method is used to get a string in any string starting at a specified index until the end of the string or starting from one index to another in any String.

This method also has an overload method.

For example:

Result:

Methods of the String object in Java

You should note that the substring() method from one index to the other does not return the character at the other index. In my example, although I call substring 1 to 2, the character at index 2 (“a”) has not been returned.


trim() method

If your strings contain more than one blank or double-spaced character or tab, and now you want to delete them, use the trim() method.

For example:

Result:

Methods of the String object in Java


replace() method

Used to create another new String object, by replacing all characters or strings in any string with the character or string passed in.

For example:

Result:

Methods of the String object in Java


length() method

Used to determine the length of any string.

For example:

Result:

Methods of the String object in Java


startsWith() method

This method used to check our string whether it begins with the character or string passed in this method?

The return value of this method is TRUE or FALSE.

For example:

Result:

Methods of the String object in Java


endsWith() method

Similar to the startsWith() method, but this method is intended to check whether our string must end with a character or a string passed in the method.

The return value of this method is also TRUE or FALSE.

For example:

Result:

Methods of the String object in Java


split() method

The split() method is used to split a certain string by using the separator character in the string. It will return us an array containing the values of the string after splitting.
For example, I have the following string “ab|cd|e”, to split this string with a separator “|”, I will code the following:

Result:

Methods of the String object in Java

In case there are separators at the end of the string but the value between these separators is empty, to return these empty values fully, you need to use the overload method of this split() method with one more parameter has a negative value. Looking at the following example, you will clearly see the difference:

Result:

Methods of the String object in Java

Add Comment