A String is a sequence of characters. Strings are immutable, which means that once a string is created, it cannot be modified.There are many methods available in the String class that you can use to manipulate strings. For example, you can use the length() method to get the length of a string, the charAt() method to get the character at a specific index in the string, and the substring() method to get a sub-string of the original string.
Strings are used frequently in Java programs to represent text, such as user input, program output, and data stored in text files.
To know more about String Class in java read the complete article.
String toUpperCase() Method:
The toUpperCase() method is a method of the String class in Java that converts all the characters in a string to their uppercase equivalent. If a character does not have an uppercase equivalent, it is not modified.The toUpperCase() method uses the default Locale to convert the characters in the string.
The toUpperCase() method does not modify the original string. It returns a new string with the uppercase characters.
Returns the String, converted to uppercase.
Note: Converts all of the characters in this String to upper case using the rules of the default locale. This method is equivalent to toUpperCase(Locale.getDefault()).
public class Main {
public static void main(String[] args) {
// Create a string with lowercase and uppercase characters
String str = "Hello World!";
// Convert the string to uppercase using the default Locale
String upperCaseStr = str.toUpperCase();
System.out.println(upperCaseStr); // Outputs "HELLO WORLD!"
// Convert the string to uppercase using a specific Locale
upperCaseStr = str.toUpperCase(Locale.FRENCH);
System.out.println(upperCaseStr); // Outputs "HELLO WORLD!"
// Convert the string to uppercase using a custom Locale
Locale customLocale = new Locale("tr", "TR");
upperCaseStr = str.toUpperCase(customLocale);
System.out.println(upperCaseStr); // Outputs "HELLO WORLD!"
}
}
Output
HELLO WORLD!
HELLO WORLD!
Explanation:In this example, we first convert the string to uppercase using the default Locale. Then, we convert it to uppercase using a specific Locale (French). Finally, we create a custom Locale (Turkish) and use it to convert the string to uppercase.
Note that the toUpperCase() method only works with ASCII and Latin-1 characters. If you want to convert characters in other scripts, you will need to use a different method or library.
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read a string from the user
System.out.print("Enter a string: ");
String str = scanner.nextLine();
// Convert the string to uppercase
String upperCaseStr = str.toUpperCase();
// Print the uppercase string
System.out.println("Uppercase string: " + upperCaseStr);
}
}
Output
Enter a string:a
Uppercase string: A
Explanation:In this example, we read a string from the user and then convert it to uppercase using the toUpperCase() method. Finally, we print the uppercase string.
Note that the toUpperCase() method does not modify the original string.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment