Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-28-2008, 03:20 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-28-2008, 04:20 PM
Member
 
Join Date: Jan 2008
Posts: 20
JAdmin is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-28-2008, 04:28 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
String handling
Hello bugger.

I wrote this program for you:
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(); } 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; } protected static void pause(){ Scanner scanner = new Scanner(System.in); scanner.nextLine(); } }
The blue method should help you.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-28-2008, 04:32 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-28-2008, 06:06 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Thanks all of you. I appreciate this.

I just tried tim's code. Works fine

Thanks a dozen.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with operator in case jimJohnson New To Java 2 03-21-2008 09:22 PM
String manipulation example (Title case) Java Tip Java Tips 0 01-29-2008 10:04 AM
Hiding the frame’s title bar Java Tip Java Tips 0 12-21-2007 09:41 AM
Can I set a range in case statement? christina New To Java 1 07-25-2007 09:41 PM
How to convert a String to upper case Valeriano New To Java 2 06-28-2007 06:45 AM


All times are GMT +3. The time now is 06:05 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org