Java - String

Java

Share

In this tutorial we will learn about the String class in Java programming language.

What is a String?

A string is a sequence of characters enclosed in double quotes.

To store string value in Java we take help of the String class.

So, all the string values that we create are actually objects of the String class.

Syntax

String variableName = "Some string value.";

Where, String is the String class that we use to create the string. variableName is the name of the variable that will hold the string value.

"Some string value." within double quotes is a String constant i.e. a string value that is assigned to the string variable variableName.

Points to note

Following are the points to note about String.

  • String is a class.
  • String values like "Hello World" are String objects.
  • String are immutable meaning, once a String object is created then, its content can't be changed.
  • To concatenate two strings we use the + operator.

String methods

String class provides us several methods that we can use. Some of them are listed below.

Method Description
length() This tells us about the length of the string i.e. total number of characters in the string.
str.charAt(index) This returns the character at the given index of the string str.
Note! First character of a string gets the index 0.
str1.equals(str2) To check the equality of two strings str1 and str2.
Returns true if two strings are equal otherwise, false.
toCharArray(str) This will converter the string str to a character array.
toLowerCase(str) Returns a string in lower case.
toUpperCase(str) Returns a string in upper case.

Example #1: Write a program in Java to print the characters in a given string

This problem can be solved using charAt() and length() method and using for loop.

class Example {
  public static void main(String[] args) {
    
    // string
    String str = "Hello World";

    // total characters
    int len = str.length();

    // print characters
    for (int i = 0; i < len; i++) {
      System.out.println("Index: " + i + "\tChar: " + str.charAt(i));
    }

  }
}

Output:

$ javac Example.java 
$ java Example
Index: 0	Char: H
Index: 1	Char: e
Index: 2	Char: l
Index: 3	Char: l
Index: 4	Char: o
Index: 5	Char:  
Index: 6	Char: W
Index: 7	Char: o
Index: 8	Char: r
Index: 9	Char: l
Index: 10	Char: d

Example #2: Write a program in Java to convert a given string into array and print the content of the array

To convert a string into a character array we can use the toCharArray() method and then use the for loop to print the content of the array.

class Example {
  public static void main(String[] args) {
    
    // string
    String str = "Hello World";

    // character array
    char[] chArr = str.toCharArray();

    // print the content
    for (char ch: chArr) {
      System.out.println(ch);
    }

  }
}

Output:

$ javac Example.java 
$ java Example
H
e
l
l
o
 
W
o
r
l
d

Example #3: Write a program in Java to convert a given string to lower case

class Example {
  public static void main(String[] args) {
    
    // string
    String str = "Hello World";

    // to lower case
    String strLowerCase = str.toLowerCase();

    // output
    System.out.println(strLowerCase);

  }
}

Output:

$ javac Example.java 
$ java Example
hello world
Share