string methods in java

In java, String class has a lot of methods that are used to operate string. In this post, we will see some important methods of String class or string methods in java We will cover each method with example and also discuss the best practices of the java string method and string methods in java

string methods in java

String trim()

This trim() method of String class is used to eliminate the leading or trailing space of string. This method removes the extra space before and after the string. The return type of trim() is String and it doesn’t take any parameter. To get more detail of the trim() method with example you can read trim method in java.

String length()

This method is used to check the length of the current string. It returns the count of the total number of characters. The return type of length() is int. It returns the number of characters present in the string. To get more details about the java string length, you can read the  length() function in java and find length of string java

String replace()

The replace() method is used to replace the content of a string with other content. You can replace the old characters/substring to the new characters/substrings. It means by use of this method you can replace the old content(old characters) with the new contents(new contents). The return type of replace() is String. It returns the String after the replacement operation of character. To get more details we can read java replace method and java string replaceall.

String isEmpty()

The string empty method is used to check the current string is empty or not. It returns true if the length of the string is greater than 0 otherwise it returns false. The return type of isEmpty() is boolean. It returns only a true or false value based on the length of the string. To get more details we can read java string empty. 

String spilt()

The split method in java is used to split the String into Sting array based on given regex. You can break down a string into multiple strings by use of the split method. Basically, it split the string into chunks based on given regex. This method returns the String array. . To get more details  we can read split function in java and see how to split a string in java.

String substring()

A string is a group of characters or an array of characters. In java substring can be part of String or whole string as well. On the other hand, we can say substring is a subset of String. Java provides some methods to find substring in string java.
Suppose we have a string “JavaGoal”, it has a number of substrings like “Java”, “Goal”, “va”, “ja”, “JavaGoal” etc. To find substring in string java provides substring() method. To get more details we can read the substring method in java and see how to get java substring from String.

String toCharArray()

The tochararray method in java is used to converts the string into a character array(java tochararray). It returns a newly created character array. Its length is totally depending on the length string which will be converted in a char array. Its contents are initialized with the characters of the current string. Its returns type char array. It doesn’t take any parameter. To get more details you can read tochararray method in java.

String toLowerCase()

The toLowerCase() method is used to convert the string into lower case letters. It converts all the letters into the lower case of the string. The return type of toLowerCase() is String. To get more details you can read toLowerCase()  method.

String toUpperCase()

String toUpperCase() method is used to convert the string into upper case letters. It converts all the letters into the upper case of the string. The return type of toUpperCase() is String. To get more details you can read toUpperCase()  method

String intern()

intern method in java returns the string from the memory. It is used for getting the string from memory if it is already present. As you know whenever we create a String literal the JVM checks the string in memory. If it is existing in memory, then it returns the string otherwise creates a new string in memory. It means string literal is using intern() method. To get more details you can read string intern method.

String valueOf()

string valueof() method is used to converts different types of values into a string. You can convert different type of data value into String data value. To get more details you can read java string valueof().

String join()

join method in java is used to join the delimiter with string. This method returns a string joined with the given delimiter. You can join multiple strings with the same delimiter. The delimiter is copied for each string. The return type of join() is String. It returns the String joined with the given delimiter. To get more details  you can read java string join.

String indexOf()

This method is used to get the index of the specified character. You can get the index of any character or substring. If the given character or substring is doesn’t exist in the current string, it returns -1. The return type of indexOf() is int. It always returns an integer number. It returns the String joined with the given delimiter. To get more details you can read string indexOf in java.

String lastIndexOf()

The lastindexof() method is used to get the last index of the specified character. You can get the last index of any character or substring. If the given character or substring is doesn’t exist in the current string, then it returns -1. The return type of lastIndexOf () is int. It always returns an integer number. The lastIndexOf() is an overloaded method in the String class. To get more details you can read string lastIndexOf in java.

String getBytes()

This method is used to get the byte array of string. It returns the array of byte. It has two variants:

1. String getBytes()
2. String getBytes(String charsetName)

To get more details you can read string getBytes() in java.

String getChar()

This method is used to copy the current string into a character array. You must provide the char array to the method in which you want to copy the content current string. To get more details you can read string getChar() method

String startsWith()

string startswith() method is used to check the current string is starts with a given string. It means the method returns true if the current string starts with suffix otherwise it returns false.To get more details you can read string startwith() method

string endswith()

string endswith method is used to check the current string is ends with a given string. It means the method returns true if the current string ends with suffix otherwise it returns false. The return type of endsWith() is boolean. It returns only a true or false value. To get more details you can read string endsWith()

String equal()

In java, the equals() method is defined in the Object class and the String class is overriding the equals() method. In String class equals() method is used for String comparison in Java. We can compare two string by use of equals() method. It compares the values of string for equality. To get more details you can read string equals()

String equalsIgnoreCase()

You can compare two string by use of equalsIgnoreCase() method. As you know java is case sensitive language and you want to ignore case sensitive string. You just want to compare the string value then you should use the equalsIgnoreCase(). It is also used for String comparison in Java and its returns type is boolean. To get more details you can read string equalsIgnoreCase()

String contains()

java string contains method is used to search the sequence of characters in a string. It means you can search for any number of characters. Even you can search for a single character or substring. It returns true if a sequence of characters is found otherwise false. The return type of contains() is boolean. It returns only true or false value. To get more details you can read string contains().

String charAt()

string charat() method is used to get the character at the specified index. The index number should be between the 0 to length-1. If length is not lies between 0 to length-1 it will throw StringIndexOutOfBoundsException. To get more details you can read String charAt()

String compareTo()

java compareTo method is used to compare the strings. It is used to compare the given string with the current string. This method compares the string-based of lexicographic. Lexicographic comparisons are like the ordering that one might find in a dictionary. The return type of compareTo() is int. It returns positive, negative or zero(0) value. To get more details you can read String compareTo()

Leave a Comment