LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-05-2004, 05:58 AM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Rep: Reputation: 30
Java: Change first character in word to upper


Hey all

I'm kinda new to Java programming, used to working in C lol...What I want to do is convert a users input to LowerCase and then change the first character of the string to upper...

i.e.
Code:
public class MakeLower {
  public static void main(String[] arguments) {
    String firstName = "BOB";
    int nameLength = firstName.length();
                                                                                                                               
    firstName = firstName.toLowerCase();
                                                                                                                               
    System.out.println("Size of " + firstName + " is " + nameLength);
  }
}
Now after the toLowerCase I need to use a toUpperCase but I think i'll need an array to break it up... not sure in Java

Can anyone help?
 
Old 04-05-2004, 06:48 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
as a rough guess how about
Code:
s.substring(0,1).toUpperCase() + s.substring(1).toLowerCase()
 
Old 04-05-2004, 06:52 AM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Original Poster
Rep: Reputation: 30
Works perfectly

Thanks heaps Kev
 
Old 04-05-2004, 03:06 PM   #4
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Original Poster
Rep: Reputation: 30
Hmmm k just thinking about that one, it's only going to change the first character in the string... What about if someone used "BOB FOO" - i'tll only change it to "Bob foo"...

What would be the best way to get both names to be capital? Spose i'd need to separate the string into sepearte strings, change it then concatenate the string again?
 
Old 04-05-2004, 03:16 PM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
sorry, from your first post i thought you only wanted the first character of the string to be capital not the first character of every word in the string.

im not so sure about this as my java knowledge is more improvisational than anything else but i think youve got the general idea, it should look something like

Code:
String out="";
StringTokenizer st = new StringTokenizer("whatever you want"," ");
while(st.hasMoreTokens()) {
    String s=st.nextToken();
    out += s.substring(0,1).toUpperCase() + s.substring(1).toLowerCase();
}

Last edited by kev82; 04-05-2004 at 03:18 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change default character encoding alepou192 Ubuntu 2 05-23-2005 04:56 AM
change word during boot bruse Linux - Newbie 4 02-11-2005 08:24 AM
word wrap in java linux_ub Programming 3 08-11-2004 01:16 AM
How to change character device file name? taphos Linux - General 2 03-28-2004 08:12 AM
function won ' t return character array word Linh Programming 1 07-31-2003 06:11 PM


All times are GMT -5. The time now is 02:48 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration