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.
String manipulation involves performing operations on strings to change or analyze their content. Here are some common string operations:
Concatenation: Joining two or more strings together.
Length: Finding the number of characters in a string.
Substring: Extracting a part of a string.
Character Extraction: Getting a specific character from a string.
Case Conversion: Changing the case of characters in a string.
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
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
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
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
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!
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);
}
}
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);
}
}
Write a program that takes a user's full name (first and last name) and prints:
The total number of characters in the full name.
The initials of the user.
The full name in all upper case.
A message saying "Welcome, [full name]!"
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!
Use length()
to find the total number of characters.
Use charAt(0)
to get the initials.
Use toUpperCase()
to convert the name to upper case.
Use concatenation to form the welcome message.
Happy Coding!