Home

AAP-2.D Evaluate expressions that manipulate strings

Understanding String Manipulation in Java

What is a String?

In Java, a string is a sequence of characters. For example, "Hello, World!" is a string. Strings are used to store and manipulate text in a program. In Java, strings are objects of the String class.

Evaluating Expressions that Manipulate Strings

String manipulation involves performing operations on strings to change or analyze their content. Here are some common string operations:

  1. Concatenation: Joining two or more strings together.

  2. Length: Finding the number of characters in a string.

  3. Substring: Extracting a part of a string.

  4. Character Extraction: Getting a specific character from a string.

  5. Case Conversion: Changing the case of characters in a string.

1. Concatenation

Concatenation means joining two strings together. In Java, you can use the + operator to concatenate strings.

String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName); // Output: John Doe

2. Length

The length() method returns the number of characters in a string.

String message = "Hello, World!";
int length = message.length();
System.out.println(length); // Output: 13

3. Substring

The substring() method extracts a part of a string. You can specify the starting index and optionally, the ending index.

String message = "Hello, World!";
String hello = message.substring(0, 5); // From index 0 to 4
System.out.println(hello); // Output: Hello

4. Character Extraction

The charAt() method returns the character at a specific index.

String message = "Hello, World!";
char firstChar = message.charAt(0);
System.out.println(firstChar); // Output: H

5. Case Conversion

The toUpperCase() and toLowerCase() methods convert all characters in a string to upper or lower case, respectively.

String message = "Hello, World!";
String upperCaseMessage = message.toUpperCase();
String lowerCaseMessage = message.toLowerCase();
System.out.println(upperCaseMessage); // Output: HELLO, WORLD!
System.out.println(lowerCaseMessage); // Output: hello, world!

Real-World Example: Creating a Username

Let's say you are creating a program where a user enters their first and last name, and the program generates a username by concatenating the first name and the first letter of the last name.

import java.util.Scanner;

public class UsernameGenerator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter your first name: ");
        String firstName = scanner.nextLine();

        System.out.println("Enter your last name: ");
        String lastName = scanner.nextLine();

        String username = firstName + lastName.charAt(0);

        System.out.println("Your username is: " + username);
    }
}

Full Program: String Manipulation

Here's a full Java program that demonstrates various string manipulation techniques:

import java.util.Scanner;

public class StringManipulation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Ask the user for input
        System.out.println("Enter a message: ");
        String message = scanner.nextLine();

        // Display the length of the message
        int length = message.length();
        System.out.println("The length of the message is: " + length);

        // Display the first character of the message
        char firstChar = message.charAt(0);
        System.out.println("The first character is: " + firstChar);

        // Display a substring of the message
        String substring = message.substring(0, 5);
        System.out.println("The first 5 characters are: " + substring);

        // Convert the message to upper case and lower case
        String upperCaseMessage = message.toUpperCase();
        String lowerCaseMessage = message.toLowerCase();
        System.out.println("Upper case: " + upperCaseMessage);
        System.out.println("Lower case: " + lowerCaseMessage);

        // Concatenate the message with another string
        String newMessage = message + " Have a great day!";
        System.out.println("Concatenated message: " + newMessage);
    }
}

Programming Problem

Write a program that takes a user's full name (first and last name) and prints:

  1. The total number of characters in the full name.

  2. The initials of the user.

  3. The full name in all upper case.

  4. A message saying "Welcome, [full name]!"

Example Output

Enter your first name: John
Enter your last name: Doe
Total number of characters: 7
Initials: J.D.
Full name in upper case: JOHN DOE
Welcome, John Doe!

Tips

Happy Coding!