Results 1 to 7 of 7
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default String Title case

    Hi,

    I use toUpperCase() adn toLowerCase() when needed. I want a String to be in title case, that is, first letter in caps and the remaining in lower case.

    Is there a way of doing it?

    Cheers.

  2. #2
    JAdmin is offline Member
    Join Date
    Jan 2008
    Posts
    20
    Rep Power
    0

    Default

    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

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default String handling

    Hello bugger.

    I wrote this program for 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();
    	}
    }
    The blue method should help you. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Quote Originally Posted by JAdmin View Post
    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!
    I understood the problem differently. Bugger, you can choose which reply will help you. My code will capitalize the first character in a string. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #5
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks all of you. I appreciate this.

    I just tried tim's code. Works fine :)

    Thanks a dozen.

  6. #6
    dmjones500 is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Re: String Title case

    Also see WordUtils.capitalize() from Apache Commons Lang.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,348
    Blog Entries
    7
    Rep Power
    20

    Default Re: String Title case

    Quote Originally Posted by dmjones500 View Post
    Also see WordUtils.capitalize() from Apache Commons Lang.
    Please let this old, dead thread rest. (I'm closing it now)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. How to convert a String to upper case
    By Valeriano in forum New To Java
    Replies: 16
    Last Post: 03-01-2010, 12:39 PM
  2. problem with operator in case
    By jimJohnson in forum New To Java
    Replies: 2
    Last Post: 03-21-2008, 08:22 PM
  3. String manipulation example (Title case)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 09:04 AM
  4. Hiding the frameís title bar
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 08:41 AM
  5. Can I set a range in case statement?
    By christina in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •