944,073 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1529
  • Java RSS
Jan 31st, 2006
0

String class help

Expand Post »
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 ..
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BaileyFree is offline Offline
7 posts
since Jan 2006
Jan 31st, 2006
0

Re: String class help

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jan 31st, 2006
0

Re: String class help

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BaileyFree is offline Offline
7 posts
since Jan 2006
Jan 31st, 2006
0

Re: String class help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BaileyFree is offline Offline
7 posts
since Jan 2006
Feb 1st, 2006
0

Re: String class help

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: problem in installing jdbc...
Next Thread in Java Forum Timeline: Reading in a .dat file from SSH shell command line





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC