Home
>
Java > Uppercasing First Letters of Words, Sentences or Lines in Java
Uppercasing First Letters of Words, Sentences or Lines in Java
Hi again! Today, I’m going to give you a JAVA function that uppercases first letters of each word, sentence or line. This function is also very similar with the function which was given at this article: Making uppercase the first letters of words, sentences and lines in ASP. To understand the algorithm, take a look that.
I will name my function UppercaseTheFirstLetter() again. I like that.
Function 1.0: public static String UppercaseTheFirstLetter(String string, int option)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public static String UppercaseTheFirstLetter(String string, int option){
String splitby = null,uppercased=null,output=null;String[] parts;int i; //Variables are defined
if(option==0){splitby=" ";}
else if(option==1){splitby="\\.";} //to define the dot string we used 2 backslashes.
else if(option==2){splitby="\n";} //to define new line string we used \n
string=string.trim ();
parts = string.split(splitby);
for(i=0;i<parts.length;i++){
parts[i]=parts[i].trim();
if(parts[i].length()>0){
uppercased=parts[i].substring(0,1).toUpperCase()+parts[i].substring(1,parts[i].length());
if(i>0){
output+=splitby.replace("\\", "")+uppercased;
//if splitby criteria is a dot we need to clear the backslash \.
}else{
output=uppercased;
}
}
}
return output;
} |
This function is an avoid function that makes the first letters of all separated words, sentences or lines uppercased, then
returns the string.
Usage of the UppercaseTheFirstLetter () Function
Example 1.0: uppercase.java Converting first letters of words in sentences to uppercase.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class uppercase {
public static String UppercaseTheFirstLetter(String string, int option){
String splitby = null,uppercased=null,output=null;String[] parts;int i; //variables are defined
if(option==0){splitby=" ";}
else if(option==1){splitby="\\.";}
else if(option==2){splitby="\n";}
string=string.trim();
parts = string.split(splitby);
for(i=0;i<parts.length;i++){
parts[i]=parts[i].trim();
if(parts[i].length()>0){
uppercased=parts[i].substring(0,1).toUpperCase()+parts[i].substring(1,parts[i].length());
if(i>0){
output+=splitby.replace("\\", "")+uppercased;
}else{
output=uppercased;
}
}
}
return output;
}
public static void main(String[] args) {
String example=" i am not going to crazy because of programming anymore! thanks mEMISO ";
System.out.println("Original: "+example+"\n-----------\n");
System.out.println("Output: "+UppercaseTheFirstLetter(example,0));
}
} |
Output of Example 1.0:
1 2 3 4
| Original: i am not going to crazy because of programming anymore! thanks mEMISO
-----------
Output: I Am Not Going To Crazy Because Of Programming Anymore! Thanks MEMISO |
Example 2.0: uppercase.java Converting first letters of sentences to uppercase.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class uppercase {
public static String UppercaseTheFirstLetter(String string, int option){
String splitby = null,uppercased=null,output=null;String[] parts;int i; //variables are defined
if(option==0){splitby=" ";}
else if(option==1){splitby="\\.";}
else if(option==2){splitby="\n";}
string=string.trim();
parts = string.split(splitby);
for(i=0;i<parts.length;i++){
parts[i]=parts[i].trim();
if(parts[i].length()>0){
uppercased=parts[i].substring(0,1).toUpperCase()+parts[i].substring(1,parts[i].length());
if(i>0){
output+=splitby.replace("\\", "")+uppercased;
}else{
output=uppercased;
}
}
}
return output;
}
public static void main(String[] args) {
String example=" the fear of death follows from the fear of life. a man who lives fully is prepared to die at any time. ";
System.out.println("Original: "+example+"\n-----------\n");
System.out.println("Output: "+UppercaseTheFirstLetter(example,1));
}
} |
Output of Example 2.0:
1 2 3 4
| Original: the fear of death follows from the fear of life. a man who lives fully is prepared to die at any time. ( Mark Twain )
-----------
Output: The fear of death follows from the fear of life.A man who lives fully is prepared to die at any time ( Mark Twain ) |
Example 3.0: uppercase.java Converting first letters of lines to uppercase.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class uppercase {
public static String UppercaseTheFirstLetter(String string, int option){
String splitby = null,uppercased=null,output=null;String[] parts;int i; //variables are defined
if(option==0){splitby=" ";}
else if(option==1){splitby="\\.";}
else if(option==2){splitby="\n";}
string=string.trim();
parts = string.split(splitby);
for(i=0;i<parts.length;i++){
parts[i]=parts[i].trim();
if(parts[i].length()>0){
uppercased=parts[i].substring(0,1).toUpperCase()+parts[i].substring(1,parts[i].length());
if(i>0){
output+=splitby.replace("\\", "")+uppercased;
}else{
output=uppercased;
}
}
}
return output;
}
public static void main(String[] args) {
String example=" please, come and find me, my love\ni'm ready now, to come home\nplease, come and find me, my love\nlet's leave this place, let's leave no trace ";
System.out.println("Original: "+example+"\n-----------\n");
System.out.println("Output: "+UppercaseTheFirstLetter(example,2));
}
} |
Output of Example 3.0: My another favorite song “My Love” from Lene Marlin.
1 2 3 4 5 6 7 8 9 10
| Original: please, come and find me, my love
i'm ready now, to come home
please, come and find me, my love
let's leave this place, let's leave no trace
-----------
Output: Please, come and find me, my love
I'm ready now, to come home
Please, come and find me, my love
Let's leave this place, let's leave no trace |
Here it is! You don’t need to look anymore for uppercasing first letters of strings. You can define your own splitby criteria to split strings. Enjoy it.
um.. First, it’s Java, not JAVA. Do you write MAC instead of Mac as well?
Second, class names should be capitalized.
Oh yeah, class names. It’s a class – which brings be to my third point: it’s a method, not a function.
First of all, Thanks for warnings.
My major purpose was giving solution for this issue, so i didn’t focused on these points but you are right.. Cautions will be taken into consideration;)
Yes, Mehmatrix, please do bother to proof read your posts next time.
Thank you for taking the criticism well.