|
String manipulation is one of the easiest process in Java, yet confusing sometimes. There are many build-in methods available with String class itself. Manipulating words in a String can be done by using java.text.BreakIterator. BreakIterator can be used to find the location of boundaries in text. The below example shows how to break a String into words.
import java.text.BreakIterator;
BreakIterator wordBreaker = BreakIterator.getWordInstance();
String str = "life is good";
wordBreaker.setText(str);
int end = 0;
for(int start = wordBreaker.first();
(end= wordBreaker.next()) != BreakIterator.DONE; start=end){
String word = str.substring(start, end)
//System.out.println(word);
}
The code above becomes useful, when we need to get a list of words in a String, to capitalize first letter in each word, to get first letter of each word etc.
Hope this helps!
__________________
Sincerely, Your friends at To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by JAdmin : 01-28-2008 at 04:24 PM.
|