String class help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2006
Posts: 7
Reputation: BaileyFree is an unknown quantity at this point 
Solved Threads: 0
BaileyFree BaileyFree is offline Offline
Newbie Poster

String class help

 
0
  #1
Jan 31st, 2006
hi I have some problem..

for instance i have a String s= blue
it maybe another string of yellow red whatever

how do i convert that b to B

i only read about s.charAt(0) to get the first letter ..
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: String class help

 
0
  #2
Jan 31st, 2006
You could add 32 or subtract 32 from the ascii value depending on whether it's lower or uppercase.... A simpler solution is creating a substring and calling the toUpperCase() method:

String newString = (oldstring.substring(0,1)).toUpperCase();


NOTE: It's not tested so don't be suprised if that doesn't work.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 7
Reputation: BaileyFree is an unknown quantity at this point 
Solved Threads: 0
BaileyFree BaileyFree is offline Offline
Newbie Poster

Re: String class help

 
0
  #3
Jan 31st, 2006
Originally Posted by server_crash
You could add 32 or subtract 32 from the ascii value depending on whether it's lower or uppercase.... A simpler solution is creating a substring and calling the toUpperCase() method:

String newString = (oldstring.substring(0,1)).toUpperCase();


NOTE: It's not tested so don't be suprised if that doesn't work.

let's say I have String b = blue
after I used
String newString = (blue.substring(0,1)).toUpperCase();
the new variable is only the "B"
what steps do i actually have to do to find the other words back.. sorry I have just started on java, not really sure with the class String
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 7
Reputation: BaileyFree is an unknown quantity at this point 
Solved Threads: 0
BaileyFree BaileyFree is offline Offline
Newbie Poster

Re: String class help

 
0
  #4
Jan 31st, 2006
hey ya I tried like
String updating = "blue" <=== maybe orange or black etc..
char newString = (updating.substring(0,1)).toUpperCase().charAt(0);
updating.replace(updating.charAt(0),newString);

it still doesn't work.. kinda weird.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: String class help

 
0
  #5
Feb 1st, 2006
Strings are final. You can't change them once they're created, so you're going to have to create a new String based on the old one.

In your specific case you could use String newString = s.replace('b', 'B'); but keep in mind that that will replace ALL lowercase b's in the string with uppercase ones, and of course work only on lowercase b's.

A more generic solution would be something along the lines of String newString = s.substring(0,1).toUpperCase()+s.substring(1); but that will get rather slow if you call it a lot and can use up a lot of memory.

A better solution if you use it a lot would be to create a function that turns the string into a character array, update the first character of that (if it is within the range of lowercase letters), and return a new String generated from that character array.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1418 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC