➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
StringUtils capitalize() Method in Java | In this blog, we will discuss how to capitalize the string by using built-in methods in StringUtils class. There is a method called capitalize() in StringUtils class which will capitalize only the first character in the string.
StringUtils.capitalize() Method
The syntax for the capitalize method is as follows:-
public static String capitalize(final String str)
It capitalizes a String changing the first character to the title case, no other characters are changed.
Parameter: str, the first letter string needed to be capitalized.
Return type: string
Returns: the capitalized string.
An example of capitalize method is as follows:-
Example-1:
String: “hello”
After capitalizing only the first letter using StringUtils.capitalize() = “Hello”
Example-2:
String: “apple”
After capitalizing only the first letter using StringUtils.capitalize() = “Apple”
Example-3:
String: “mAngo”
After capitalizing only the first letter using StringUtils.capitalize() = “MAngo”
StringUtils.capitalize() Example in Java
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String string = "banana";
System.out.println("String: " + string);
System.out.println("StringUtils.capitalize(string): "
+ StringUtils.capitalize(string));
string = "aPRICOT";
System.out.println("String: " + string);
System.out.println("StringUtils.capitalize(string): "
+ StringUtils.capitalize(string));
string = "PIne apple";
System.out.println("String: " + string);
System.out.println("StringUtils.capitalize(string): "
+ StringUtils.capitalize(string));
string = "tOmato";
System.out.println("String: " + string);
System.out.println("StringUtils.capitalize(string): "
+ StringUtils.capitalize(string));
}
}
Output:-
String: banana
StringUtils.capitalize(string): Banana
String: aPRICOT
StringUtils.capitalize(string): APRICOT
String: PIne apple
StringUtils.capitalize(string): PIne apple
String: tOmato
StringUtils.capitalize(string): TOmato
The capitalize method is used to capitalize only the first character of the string. In the above code, we have used the signature of the method “StringUtils.capitalize(String str)” and passed four different types of strings to see the output for different scenarios.
This method works the same in all the cases, the main use of this method is to make the string in heading format so that only the first letter gets capitalized. And the remaining other characters remain as it is.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!