Strings in Java
A string is basically a sequence of characters, like ‘j’, ’k’, ’l’, ’p’, ’t’ and so on. But in Java, a string is treated like an object that represents a sequence of character values and for that, the string class is used to create a string object.
String methods in Java
By using String methods, we can perform various operations on strings, such as comparison, concatenation, replacement, conversion and so on. For such operations we need the java.lang.String class that provides these facilities. Whenever you submit a form in the window, mobile, or web-based application, everything is treated as a string. So here Java strings are powerful.
The
java.lang.String class provides various methods out of which the following are some of the important methods that are generally used.
- String toUpperCase()
- String toLowerCase()
- String trim()
- String startsWith()
- String endsWith()
- String charAt()
- String length()
- String intern()
- String valueOf()
- String replace()
The other methods are as in the following:
- String compareTo()
- String compareToIgnoreCase()
- String concat()
- String contains()
- String equals()
- String equalsIgnoreCase()
- String contentEquals()
- String format()
- String getBytes()
- String indexOf()
- String isEmpty()
- String join()
- String lastIndexOf()
- String replaceAll()
- String split()
- String substring()
- String toCharArray()
- String matches()
- String hashCode()
In this section we will discuss some of the preceding important methods of Java strings.
Method: Java String toUpperCase()
This method returns the entire string in upper case letters by converting all the characters of the string into upper case letters.
It has two variants:
- String toUpperCase() or String toUpperCase(Locale.getDefault()
- String toUpperCase(Locale locale)
The first one (
String toUpperCase() is equivalent to
String toUpperCase(Locale.getDefault()) and simply converts the string in upper case letter. It internally uses the default locale. Actually
Locale.getDefault() gets the current value of the default locale for the instance of the Java Virtual Machine. The JVM sets the default locale during the start-up based on the host environment. It is used by plenty of locale-sensitive methods if no locale is explicitly specified. It can be changed using the
setDefault() method.
The second one (
String toUpperCase(Locale locale)) is a variant of the first one because it converts the string into the upper case using the rules defined by the specified locale.
Let's move to an example for better understanding. In this example, the output for both variants is shown.
- import java.util.Locale;
-
- public class UpperCase
- {
- public static void main(String args[])
- {
- String str1="see the case change in me";
- String str2="see the case change in me too";
- System.out.println(str1.toUpperCase());
- System.out.println(str2.toUpperCase(Locale.FRENCH));
- }
- }
Output
Method: Java String toLowerCase()
This method returns the entire string in lower case letters by converting all the characters of the string into lower case letters. Like the preceding method, it also has the following two variants:
- String toLowerCase() or String toLowerCase(Locale.getDefault()
- String toLowerCase(Locale locale)
Like the preceding method, the first one (
String toLowerCase() is equivalent to
String toLowerCase(Locale.getDefault()) and simply converts the string to lower case letters. It internally uses the default locale.
The second one (
String toLowerCase(Locale locale)) is the variant of the first one because it converts the string into lower case by using the rules defined by the specified locale.
Example
Let's move to an example of this method too. In this example, again the outputs for both variants are shown.
- import java.util.Locale;
-
- public class LowerCase
- {
- public static void main(String args[])
- {
- String str1="WHERE THERE IS A WILL";
- String str2="THERE IS A WAY...!!!";
- System.out.println(str1.toLowerCase());
- System.out.println(str2.toLowerCase(Locale.FRENCH));
- }
- }
Output
Method: Java String trim()
This method returns the string after removing all the leading as well as trailing white spaces present in the input string. For example “ Hey there…!!! ” would return the string “Hey there…!!!”. Actually, the Unicode value for the space character is ‘\u0020’. So the trim() method in Java strings first check for the Unicode values of space characters before and after the string and if it exists then this method eliminates the white space and returns the proper string.
Note: The trim() method doesn't eliminate the spaces present between the words.
Example
Let's understand it using an example.
- public class TrimString
- {
- public static void main(String args[])
- {
- String str1=" Triming is better ";
- System.out.println("String before trim:" +str1+" option");
- System.out.println("String after trim:" +str1.trim()+" option");
- }
- }
Output
For further reading about the other methods, click the link below.