Java Strings


Java Strings

In this tutorial, we will learn about strings in Java. We will cover the basics of string manipulation, including creating, accessing, modifying, and performing operations on strings.


What is a String

A string in Java is an object of the java.lang.String class. Strings are immutable, meaning their values cannot be changed once created. Strings are used for storing and handling text data.


Creating Strings

Strings can be created in Java using double quotes:

String str = "Hello, world!";

Strings can also be created using the String class constructor:

String str = new String("Hello, world!");


Initializing Strings

  1. Create a string variable and initialize it with a value.
  2. Print the string variable using System.out.println.

Java Program

public class Main {
    public static void main(String[] args) {
        String str = "Hello, world!";
        System.out.println(str);
    }
}

Output

Hello, world!


Accessing Characters in a String

  1. Create a string variable and initialize it with a value.
  2. Access and print individual characters using the charAt method.

Java Program

public class Main {
    public static void main(String[] args) {
        String str = "Hello";
        System.out.println(str.charAt(0)); // Accessing using charAt()
        System.out.println(str.charAt(1));
    }
}

Output

H
e


Modifying Strings

  1. Create a string variable and initialize it with a value.
  2. Strings in Java are immutable, so create a new string with the modified value.
  3. Print the modified string.

Java Program

public class Main {
    public static void main(String[] args) {
        String str = "Hello";
        str = 'J' + str.substring(1); // Modifying individual character
        str += " World!"; // Appending new characters
        System.out.println(str);
    }
}

Output

Jello World!


String Concatenation

  1. Create two string variables and initialize them with values.
  2. Concatenate the strings using the + operator or the concat method.
  3. Print the concatenated string.

Java Program

public class Main {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = " World!";
        String str3 = str1 + str2; // Concatenating strings
        System.out.println(str3);
    }
}

Output

Hello World!


Finding Substrings

  1. Create a string variable and initialize it with a value.
  2. Use the indexOf method to find a substring.
  3. Print the position of the found substring.

Java Program

public class Main {
    public static void main(String[] args) {
        String str = "Hello, world!";
        int pos = str.indexOf("world"); // Finding substring
        if (pos != -1) {
            System.out.println("Found 'world' at position: " + pos);
        } else {
            System.out.println("Substring not found");
        }
    }
}

Output

Found 'world' at position: 7


String Length

  1. Create a string variable and initialize it with a value.
  2. Use the length method to get the length of the string.
  3. Print the length of the string.

Java Program

public class Main {
    public static void main(String[] args) {
        String str = "Hello, world!";
        System.out.println("Length of the string: " + str.length()); // Getting string length
    }
}

Output

Length of the string: 13