Java – Strings
Java Strings
Strings are one of Java’s most popular data types. It’s a text storage device. Characters can be encased in double quote marks to make it. The =
symbol can be used to assign it to a variable.
String MyString = "Hello World!";
String Length
In Java, a String is an object, and the String object provides a length()
function that may be used to calculate the total number of characters in the String.
public class MyClass { public static void main(String[] args) { String MyString = "Hello World!"; System.out.println(MyString.length()); } }
The output of the above code will be:
12
Check a character in the String
The indexOf()
function in Java String returns the index position of the first occurrence of the provided text in the string. Please keep in mind that Java starts counting index positions from 0.
public class MyClass { public static void main(String[] args) { String MyString = "Hello World!"; System.out.println(MyString.indexOf("Wor")); } }
The above code will give the following output:
6
String Concatenation
The +
operator in Java can be used to connect two strings. Additionally, the concat()
function in Java String may be used to merge two strings.
public class MyClass { public static void main(String[] args) { String text_1 = "Learn "; String text_2 = "Java "; String text_3 = "with TechDecodeTutorials.com"; System.out.println(text_1 + text_2 + text_3); System.out.println(text_1.concat(text_2).concat(text_3)); } }
The above code will give the following output:
Learn Java with TechDecodeTutorials.com
Learn Java with TechDecodeTutorials.com
Special characters in a string
The backslash \
escape character is used to transform special characters into string characters, such as single quotes, double quotes, newlines, and so on. Special characters in Java are described in the table below:
Escape Character | Result | Example |
---|---|---|
\’ | ‘ | “\’Java\'” is converted into: ‘Java’ |
\” | “ | “\”World\”” is converted into: “World” |
\\ | \ | “A\\C” is converted into: A\C |
\n | new line | “Hello\nJohn” is converted into: Hello John |
\t | Tab | “Hello\tMarry” is converted into: Hello Marry |
\b | Backspace | “Hello J\bava” is converted into: Hello ava |
\r | Carriage Return | “Hello\rJohn” is converted into: Hello John Note: Carriage return is not the same as the new line. It simply means a return to the left margin. |
String Methods
A handful of the most popular string techniques are explained below.
- toLowerCase():Returns string in lowercase
- toUpperCase():Returns string in uppercase
- trim():Removes whitespaces from start and end of the string
- replace(): replace specified character(s) with another specified character(s)
Example: toLowerCase() and toUpperCase() String Methods
public class MyClass { public static void main(String[] args) { String MyString = "Learn Java"; System.out.println(MyString.toLowerCase()); System.out.println(MyString.toUpperCase()); } }
The outcome of the code above is as follows:
learn java
LEARN JAVA
Example: trim() and replace() String Methods
public class MyClass { public static void main(String[] args) { String MyString = " Hello World! "; System.out.println(MyString.trim()); System.out.println(MyString.replace("World!", "Java")); } }
The above code will give the following output:
Hello World! Hello Java
Click here to open an online compiler.