I need to make my string sentence to be capitalize on each first character of word, So far haven't find such function that I can use. So made this simple class, incase someone need it. It's GPL so free to use it and modify it.
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Iterator;
import java.lang.StringBuilder;
/**
*
* @author avenpace
* I'm using singleton here so, hope you know what that means ;)
* license under the GPL so free to use, modify and hack whatever you want
* lemme know if this programme benefit you if you had the time
*/
public class CapitalizeWords {
private static CapitalizeWords capinstance;
private CapitalizeWords() {
}
/**
* The method will capitalizing every first character on each word on the String sentence
* This method take two parameter,
* @param source source sentence string that you want to capitalize on the first character if each word.
* @param allword this argument is boolean type, if you give it a true value. Then it will capitalize on first character on eeah word, if you give it with false value, then it would only capitalize the first character on the sentence
*/
public synchronized String capitalize(String source, boolean allword) {
StringBuilder firstletter = new StringBuilder(); StringBuilder reminding = new StringBuilder();
StringBuilder elsource = new StringBuilder(); StringBuilder capword = new StringBuilder();
StringBuilder result = new StringBuilder();
Scanner delimiter = new Scanner(source);
ArrayList explode = new ArrayList();
delimiter.useDelimiter(" ");
while(delimiter.hasNext()){
explode.add(delimiter.next());
}
Iterator itersource = explode.iterator();
if (allword == true) {
while(itersource.hasNext()){
elsource.append(itersource.next().toString());
firstletter.append(elsource.substring(0,1));
reminding.append(elsource.substring(1));
if (itersource.hasNext()) {
capword.append(firstletter.toString().toUpperCase() + reminding.toString().toLowerCase()).append(" ");
} else {
capword.append(firstletter.toString().toUpperCase() + reminding.toString().toLowerCase().trim());
}
result.append(capword);
//reset this buzz objects of stringbuffer
capword.setLength(0);
firstletter.setLength(0);
reminding.setLength(0);
elsource.setLength(0);
}
} else if (allword == false) {
int i = 1;
while(itersource.hasNext()){
elsource.append(itersource.next().toString());
firstletter.append(elsource.substring(0,1));
reminding.append(elsource.substring(1));
if ((i == 1) && itersource.hasNext()) {
capword.append(firstletter.toString().toUpperCase() + reminding.toString().toLowerCase()).append(" ");
} else if(itersource.hasNext()) {
capword.append(elsource.toString().toLowerCase()).append(" ");
} else {
capword.append(elsource.toString().toLowerCase().trim());
}
i++;
result.append(capword);
//reset this buzz objects of stringbuffer
capword.setLength(0);
firstletter.setLength(0);
reminding.setLength(0);
elsource.setLength(0);
}
}
return result.toString();
}
/**
* will give you the singleton instance of this class
*/
public static synchronized CapitalizeWords getInstance() {
if (capinstance == null) {
capinstance = new CapitalizeWords();
return capinstance;
} else {
return capinstance;
}
}
}
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Iterator;
import java.lang.StringBuilder;
/**
*
* @author avenpace
* I'm using singleton here so, hope you know what that means ;)
* license under the GPL so free to use, modify and hack whatever you want
* lemme know if this programme benefit you if you had the time
*/
public class CapitalizeWords {
private static CapitalizeWords capinstance;
private CapitalizeWords() {
}
/**
* The method will capitalizing every first character on each word on the String sentence
* This method take two parameter,
* @param source source sentence string that you want to capitalize on the first character if each word.
* @param allword this argument is boolean type, if you give it a true value. Then it will capitalize on first character on eeah word, if you give it with false value, then it would only capitalize the first character on the sentence
*/
public synchronized String capitalize(String source, boolean allword) {
StringBuilder firstletter = new StringBuilder(); StringBuilder reminding = new StringBuilder();
StringBuilder elsource = new StringBuilder(); StringBuilder capword = new StringBuilder();
StringBuilder result = new StringBuilder();
Scanner delimiter = new Scanner(source);
ArrayList explode = new ArrayList();
delimiter.useDelimiter(" ");
while(delimiter.hasNext()){
explode.add(delimiter.next());
}
Iterator itersource = explode.iterator();
if (allword == true) {
while(itersource.hasNext()){
elsource.append(itersource.next().toString());
firstletter.append(elsource.substring(0,1));
reminding.append(elsource.substring(1));
if (itersource.hasNext()) {
capword.append(firstletter.toString().toUpperCase() + reminding.toString().toLowerCase()).append(" ");
} else {
capword.append(firstletter.toString().toUpperCase() + reminding.toString().toLowerCase().trim());
}
result.append(capword);
//reset this buzz objects of stringbuffer
capword.setLength(0);
firstletter.setLength(0);
reminding.setLength(0);
elsource.setLength(0);
}
} else if (allword == false) {
int i = 1;
while(itersource.hasNext()){
elsource.append(itersource.next().toString());
firstletter.append(elsource.substring(0,1));
reminding.append(elsource.substring(1));
if ((i == 1) && itersource.hasNext()) {
capword.append(firstletter.toString().toUpperCase() + reminding.toString().toLowerCase()).append(" ");
} else if(itersource.hasNext()) {
capword.append(elsource.toString().toLowerCase()).append(" ");
} else {
capword.append(elsource.toString().toLowerCase().trim());
}
i++;
result.append(capword);
//reset this buzz objects of stringbuffer
capword.setLength(0);
firstletter.setLength(0);
reminding.setLength(0);
elsource.setLength(0);
}
}
return result.toString();
}
/**
* will give you the singleton instance of this class
*/
public static synchronized CapitalizeWords getInstance() {
if (capinstance == null) {
capinstance = new CapitalizeWords();
return capinstance;
} else {
return capinstance;
}
}
}
Comments