Using String in Java

On this note, we are going to have a look at how to use Strings in Java. Let’s give it a go together 🙂

Creating a String

In Java, you can create a string in several ways:

Using String literals

You can create a string using string literals, which are enclosed in double quotes. For example:

String message = "Hello, world!";
Using a Constructor

You can also create a string using a constructor. For example:

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

Concatenating Strings

You can concatenate two or more strings using the “+” operator or the concat() method. For example:

String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName; // Using the + operator
String greeting = "Hello, ".concat(fullName); // Using the concat() method

Comparing Strings

To compare two strings, you can use the equals() or equalsIgnoreCase() method. The equals() method compares two strings for equality, while the equalsIgnoreCase() method compares two strings for equality, ignoring case. For example:

String password = "password123";
if (password.equals("password123")) {
    System.out.println("Access granted!");
}

Retrieving a Substring

You can retrieve a substring from a string using the substring() method. The substring() method takes two parameters: the starting index and the ending index of the substring. For example:

String message = "Hello, world!";
String greeting = message.substring(0, 5); // greeting is "Hello"

Converting a String to Uppercase or Lowercase

You can convert a string to uppercase or lowercase using the toUpperCase() and toLowerCase() methods, respectively. For example:

String message = "Hello, world!";
String uppercaseMessage = message.toUpperCase(); // uppercaseMessage is "HELLO, WORLD!"
String lowercaseMessage = message.toLowerCase(); // lowercaseMessage is "hello, world!"

Finding the Length of a String

You can find the length of a string using the length() method. For example:

String message = "Hello, world!";
int length = message.length(); // length is 13

Searching for a Substring

You can search for a substring within a string using the indexOf() or lastIndexOf() method. The indexOf() method returns the index of the first occurrence of the substring, while the lastIndexOf() method returns the index of the last occurrence of the substring. For example:

String message = "Hello, world!";
int index = message.indexOf("world"); // index is 7

Complete example of using String in Java:

Here’s a complete code example that demonstrates the use of Java strings:

public class StringExample {
    public static void main(String[] args) {
        // Creating a string using a string literal
        String message1 = "Hello, world!";
        System.out.println(message1); // Output: Hello, world!

        // Creating a string using a constructor
        String message2 = new String("Goodbye, world!");
        System.out.println(message2); // Output: Goodbye, world!

        // Concatenating strings using the + operator and the concat() method
        String firstName = "John";
        String lastName = "Doe";
        String fullName = firstName + " " + lastName; // Using the + operator
        System.out.println(fullName); // Output: John Doe
        String greeting = "Hello, ".concat(fullName); // Using the concat() method
        System.out.println(greeting); // Output: Hello, John Doe

        // Comparing strings using the equals() and equalsIgnoreCase() methods
        String password = "password123";
        if (password.equals("password123")) {
            System.out.println("Access granted!");
        }

        // Retrieving a substring using the substring() method
        String message3 = "Hello, world!";
        String greeting2 = message3.substring(0, 5); // greeting2 is "Hello"
        System.out.println(greeting2); // Output: Hello

        // Converting a string to uppercase or lowercase using the toUpperCase() and toLowerCase() methods
        String message4 = "Hello, world!";
        String uppercaseMessage = message4.toUpperCase(); // uppercaseMessage is "HELLO, WORLD!"
        String lowercaseMessage = message4.toLowerCase(); // lowercaseMessage is "hello, world!"
        System.out.println(uppercaseMessage); // Output: HELLO, WORLD!
        System.out.println(lowercaseMessage); // Output: hello, world!

        // Finding the length of a string using the length() method
        String message5 = "Hello, world!";
        int length = message5.length(); // length is 13
        System.out.println(length); // Output: 13

        // Searching for a substring using the indexOf() and lastIndexOf() methods
        String message6 = "Hello, world!";
        int index = message6.indexOf("world"); // index is 7
        System.out.println(index); // Output: 7
    }
}

In the above example, we created several strings using string literals and a constructor, concatenated strings using the “+” operator and the concat() method, compared strings using the equals() and equalsIgnoreCase() methods, retrieved a substring using the substring() method, converted a string to uppercase or lowercase using the toUpperCase() and toLowerCase() methods, found the length of a string using the length() method, and searched for a substring using the indexOf() and lastIndexOf() methods.

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author