Java Tutorial Java Advanced Java References

Java - Strings



Java Strings

Strings are one of the most common data types in Java. It is used for storing text. It can be created by enclosing characters in double quotation marks. It can be assigned to a variable using = sign.

String MyString = "Hello World!";

String Length

A String in Java is an object and the String object has a method called length() which can be used to find 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

Java String has indexOf() method which returns index position of first occurrence of specified text in the string. Please note that, Java counts index position 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

In Java, two strings can be joined using + operator. Along with this, Java String has concat() method which can also be used to combine two strings.

public class MyClass {
  public static void main(String[] args) {
    String text_1 = "Learn ";
    String text_2 = "Java ";
    String text_3 = "with AlphaCodingSkills.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 AlphaCodingSkills.com
Learn Java with AlphaCodingSkills.com

Special characters in a string

The backslash \ escape character is used to convert special character like single quote, double quote, new line, etc. into the string character. The below mentioned table describes special characters in Java:

Escape CharacterResultExample
\' ' "\'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 same as new line. It simply means return to the left margin.

String Methods

For complete list of string methods, please refer to string methods page. Here, few very common string methods are discussed.

  • 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 above code will give the following output:

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