Java Strings Methods

Methods of Java String Class

The methods of String class are used to obtain information about objects and are known as accessor methods. The String class provides many accessor methods that are used to perform operations on strings. We will cover some important methods of the String class in detail with the help of examples and code.

1. int length() method

The length() method returns the length of the String that is, the number of characters in a String.

Syntax:

stringName.length( ) ;

Code to illustrate the use of length() method:

Output:

Length of sentence is : 16

Note: The length() method also includes whitespaces in the strings to calculate the length of the string.

2. char charAt(int index) method

We use charAt method to return a character value at a specified position (index) of a String.

Syntax:

stringName.charAt( index ) ;

Code to illustrate the use of charAt() method

Output:

At position 8, the character is: o
At position 1, the character is: a

3.String concat(String string1) method

The method concat() is used for concatenating or adding two strings into a single string. This method appends its String argument to the indicated string argument and returns a new instance of the String class.

Syntax:

string1.concat(string2);

Code to illustrate the use of the concat() method:

Output:

Concatenated String is: Naveen Joshuva Developer

4. String substring(int beginIndex) method

The substring method with a single argument is used to create new objects or instances of String class from existing instances of String. The method returns the substring from the index which is passed as an argument to the method.

Syntax:

sentence.substring(beginIndex) ;

Code to illustrate the use of the substring(int index) method:

Output:

The Substring is: BecomingNJ blog on  Strings

5. String substring(int beginIndex, int endIndex) method

The substring with two arguments method is used to create new objects or instances of String class from existing instances of String. The method returns the new String by using the required index range (including the start index and excluding the end index).

Syntax:

sentence.substring(beginIndex, endIndex) ;

Code to illustrate the use of the substring(beginIndex, endIndex) method:

Output:

The Substring within the range is: BecomingNJ
The Substring within the range is: Blog on 

6. int compareTo(String string1, String string2) method(need to be discussed)

7. String toUpperCase() method

The method toUpperCase() converts all the characters of a given string to uppercase.

Syntax:

sentence.toUpperCase() ;

Code to illustrate the use of the toUpperCase() method:

Output:

HELLO WORLD
WELCOME TO MYBLOG

8. String toLowerCase() method

The method toLowerCase() converts all the characters of a given string to lowercase.

Syntax:

sentence.toLowerCase() ;

Code to illustrate the use of the toLowerCase() method:

OUTPUT:

hello world
welcome to myblog

9. String trim() method

This method returns the new String after removing whitespaces at both ends. It does not affect whitespaces in the middle of the String.

Syntax:

sentence.trim() ;

Code to illustrate the use of the trim() method:

Output:

The original string is: This is a Tutorial on Strings 
The trimmed string is:This is a Tutorial on Strings

10. String replace(char oldChar, char newChar)

This method replaces each occurrence of the first argument of the string with the second argument and returns the resulting string.

Syntax:

sentence.replace(char1, char2) ;

Code to illustrate the use of the replace() method:

Output:

String after replacing characters is: Rohit Sherme struck 60 off 51 to give Indie e solid stert.

Reference:

Tech Vidvan

Leave a comment

Design a site like this with WordPress.com
Get started