In this article, we will discuss how to convert first character of every word to uppercase
For conversion, we will use either
- StringTokenizer class
- split() method of String class
Note:
- StringTokenizer is deprecated now but however it is carried forward for backward compatibility
- Instead of StringTokenizer, developer should prefer using split() method of String class
Let us move forward to discuss to convert 1st character of every word to uppercase
1. Using StringTokenizer
Steps:
- Split the sample string into tokens using default space as delimiter
- Iterate through tokens using while loop
- Store each token into temp variable in each iteration
- And convert 1st character of each token into uppercase
- And append to StringBuffer by using append() method, along with single-space
- Finally pretty print to console
ConvertFirstCharacterOfEveryWordUsingStringTokenizer.java
package in.bench.resources.sb.operation;
import java.util.StringTokenizer;
public class ConvertFirstCharacterOfEveryWordUsingStringTokenizer{
public static void main(String[] args) {
// create StringBuffer object to store converted strings
StringBuffer sbuffer = new StringBuffer();
// sample string
String str = "who will be answerable"
+ " at the end of the day";
// create StringTokenizer with above content
StringTokenizer st = new StringTokenizer(str);
while(st.hasMoreElements()) {
// store it in temporary variable
String temp = st.nextToken();
// convert 1st character into upper-case
String firstUppercase = Character.toUpperCase(
temp.charAt(0)) + temp.substring(1);
// add converted string first
sbuffer.append(firstUppercase);
// and then add single space
sbuffer.append(" ");
}
// finally pretty print to console
System.out.println(sbuffer.toString().trim());
}
}
Output:
Who Will Be Answerable At The End Of The Day
2. Using split() method of String class
Steps:
- Split the sample string by specifying space as delimiter and store it into String[] array
- Iterate through string array using for-loop
- Store each string into temp variable in each iteration
- And convert 1st character of each string into uppercase
- And append to StringBuilder by using append() method, along with single-space
- Finally pretty print to console
ConvertFirstCharacterOfEveryWordUsingSplitMethod.java
package in.bench.resources.sb.operation;
public class ConvertFirstCharacterOfEveryWordUsingSplitMethod {
public static void main(String[] args) {
// create StringBuilder object to store converted strings
StringBuilder sbuilder = new StringBuilder();
// sample string
String str = "this world has very good leader"
+ " only that they need to be identified";
// create StringTokenizer with above content
String[] strArray = str.split("\\s");
for(int index=0; index < strArray.length; index++) {
// store it in temporary variable
String temp = strArray[index];
// convert 1st character into upper-case
String firstUppercase = Character.toUpperCase(
temp.charAt(0)) + temp.substring(1);
// add converted string first
sbuilder.append(firstUppercase);
// and then add single space
sbuilder.append(" ");
}
// finally pretty print to console
System.out.println(sbuilder.toString().trim());
}
}
Output:
This World Has Very Good Leader
Only That They Need To Be Identified
Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles:
- Java – StringTokenizer class with example
- Java – Counting number of tokens using StringTokenizer
- Java – Reversing a String by word using StringTokenizer
- Java – String comparison in 3 ways
- Java – String concatenation in 2 ways
- Java – Reverse a String contents in 4 ways
- Java – Split a String contents in 3 ways
- Java – Overriding toString() method to print values of ArrayList
- Java – How to left pad with zeroes to a String
- Java – How to remove white-spaces in a String ?
- Java – How to UPPERCASE every duplicate character/occurrences in a String
- Java – Remove duplicate characters from String in Java
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/StringTokenizer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
Happy Coding !!
Happy Learning !!