Results 1 to 7 of 7
Thread: String Title case
- 01-28-2008, 02:20 PM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 111
- Rep Power
- 0
- 01-28-2008, 03:20 PM #2
Member
- Join Date
- Jan 2008
- Posts
- 20
- Rep Power
- 0
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!Last edited by JAdmin; 01-28-2008 at 03:24 PM.
Sincerely, Your friends at www.javaadvice.com
- 01-28-2008, 03:28 PM #3
String handling
Hello bugger.
I wrote this program for you:
The blue method should help you. ;)Java Code:import java.util.*; public class Main{ public static void main(String[] arg){ String test = "hEllo EveRyOne"; System.out.println("Original: " + test); System.out.println("String: " + title(test)); pause(); } [COLOR="RoyalBlue"] public static String title(String string){ String result = ""; for (int i = 0; i < string.length(); i++){ String next = string.substring(i, i + 1); if (i == 0){ result += next.toUpperCase(); } else { result += next.toLowerCase(); } } return result; }[/COLOR] protected static void pause(){ Scanner scanner = new Scanner(System.in); scanner.nextLine(); } }Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-28-2008, 03:32 PM #4
- 01-28-2008, 05:06 PM #5
Senior Member
- Join Date
- Nov 2007
- Posts
- 111
- Rep Power
- 0
Thanks all of you. I appreciate this.
I just tried tim's code. Works fine :)
Thanks a dozen.
- 01-31-2012, 12:54 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
Re: String Title case
Also see WordUtils.capitalize() from Apache Commons Lang.
- 01-31-2012, 01:21 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
How to convert a String to upper case
By Valeriano in forum New To JavaReplies: 16Last Post: 03-01-2010, 12:39 PM -
problem with operator in case
By jimJohnson in forum New To JavaReplies: 2Last Post: 03-21-2008, 08:22 PM -
String manipulation example (Title case)
By Java Tip in forum Java TipReplies: 0Last Post: 01-29-2008, 09:04 AM -
Hiding the frame’s title bar
By Java Tip in forum Java TipReplies: 0Last Post: 12-21-2007, 08:41 AM -
Can I set a range in case statement?
By christina in forum New To JavaReplies: 1Last Post: 07-25-2007, 08:41 PM


LinkBack URL
About LinkBacks


Bookmarks