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

django realtime currency rate

Its been a really long time since I post something here For some dumb reason, I forgot my blogger login/password :hammer: Anyway, since I'm now heavily work with django stuff Here are some django related projects that hopefully can benefit anyone who need it * django-currency_rate, simple django app that can give you realtime currency rate base on http://themoneyconverter.com . Owh its even can calculate the rate with templatetag :) https://github.com/avenpace/django-exchange_rate

python-oauth2 hack

When you are using python-oauth2 from simplegeo on your Google App Engine instance, you'll get some exception that cause by "ImportError: No module named httplib2" Yes, apparently httplib2 are not supported by GAE and instead they oblige you to use google.appengine.api.urlfetch.fetch instead So I hack python-oauth2 to use google.appengine.api.urlfetch.fetch You can found my python-oauth2 fork on https://github.com/avenpace/python-oauth2

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 ^_^