In the previous chapter, we learnt about Java Var Keyword. In this chapter, let us dive deeper into the String datatype in Java programming language.
“String” is a data type used in Java to store texts.
The syntax for creating a String variable is given below for reference.
String cityName = “New York”;
The data assigned to the String variable should be enclosed within double quotes.
If you want to include special characters within the enclosed text, it should be preceded with an escape character in the form of backslash (\).
E.g.
String name = “D\’caprio”;
The String keyword used here is an Object. Hence, it also contains a lot of methods which can be used to perform some actions on String data. Some of the commonly used String methods are listed below.
You can find the length of the text using the length() method as shown below:
String text = "This is text";
System.out.println(text.length()); //12
You can convert a given text to all upper case letters using the toUpperCase() method as shown below:
String text = "This is text";
System.out.println(text.toUpperCase()); //THIS IS TEXT
You can convert a given text to all lower case letters using the toLowerCase() method as shown below:
String text = "This IS Text";
System.out.println(text.toLowerCase()); //this is text
If you want to access a specific character of the String by location, then you can use charAt() method as shown below:
String text = "This IS Text";
System.out.println(text.charAt(5)); //I
If you want to compare two texts, you can use equals() method as shown below:
String color1 = "Green";
String color2 = "Red";
System.out.println(color1.equals(color2)); //false
String name1 = "Tim";
String name2 = "Tim";
System.out.println(name1.equals(name2)); //true
This method will return boolean value. It will return true when the text matches and it will return false when the text doesn’t match.
If you want to compare two texts without considering their case, then you can make use of equalsIgnoreCase() method as shown below
String color1 = "GREEN";
String color2 = "Green";
System.out.println(color1.equalsIgnoreCase(color2)); //true
String name1 = "Tim";
String name2 = "Tim";
System.out.println(name1.equals(name2)); //true
String text1 = "Green";
String text2 = "Tim";
System.out.println(text1.equals(text2)); //false
Similar to equals() method, this method will also return either true if the text matches or false when there is mismatch.
If your text contains additional whitespaces in the beginning or end, you can use trim() method to remove the extra whitespaces from both sides of the text.
String text = " This is text ";
System.out.println(":" + text.trim() + ":"); //:This is text:
If you want to concatenate or join two strings, you can use the + operator as shown below.
String text1 = "This is text1. ";
String text2 = "This is text2.";
System.out.println(text1 + text2); // This is text1. This is text2.
I hope this tutorial gave you a fair understanding of String datatype and methods in Java.
Fully customizable CRM Software for Freelancers and Small Businesses