Skip to main content

CapitalizeWords.java

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;
}
}
}

Comments

Adam said…
Nice function, it was a lot quicker than writing one from scratch!
vi5in said…
Check out Apache Commons-Lang WordUtils.

Popular posts from this blog

Yet another online shop launch

For housewife who use to work, daily activities are dull and boring. So I've been persuade my wife to run online shop to add variation on her daily routine. Finally we decided to start selling clothing goods and knick knacks, some are airbrush paint t shirt, woman snake leather handbag are there also Since I've been working with python, I came out to use satchmo for our online shop framework After doing customizing here and there and build some payment modules, we lauch WarungRupa.com  on early January 2012 We have some ideas which are still in our thoughts and we keen to work on it Hope WarungRupa.com  can have many visitors and buyers Next to do are SEO and promotions ^_^
1 Desember 2003 Spider admin diinstall dari < http://agaipiotras.igk.pl/spider-admin.xml.gz > karena situs cvs di midgard tidak dapat diakses dan situs spider sendiri berbahasa Rusia sehingga mencari di www.google.com dengan keyword "spider-admin.xml.gz". Spider dan aegir dapat berjalan dengan baik.
wheeeww... it been a while since I do my last web bloging since the web project finally hit the road plus I've only lame ass internet connection. I've decided that this blog gonna be my personal blog that would tell you guys about my experience with opensource world and maybe a bit about my presonal things (which is almost imposible since I'm not so much open about my personal life ;) ) one other thing... yeah it would be gado-gado blog entry. That mean sometime I'll post in english and sometime in bahasa due to the limitation of my brain ;)