Java String toUpperCase() Method
What is string?
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.
Syntax:
public String toUpperCase()
Let’s look at a string-related Java program where the Java String toUpperCase() Method is used to perform an operation on the given string.
Example 1 : Java String toUpperCase () Method
public class Main { public static void main(String[] args) { String txt = "Prepinsta or prepinsta prime"; System.out.println(txt.toUpperCase()); } }
Output
PREPINSTA OR PREPINSTA PRIME
Example 2 : Java string toUpperCase Method
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!
Example 3: Java String toUpperCase Method
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
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment