| | |
String class help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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.
String newString = (oldstring.substring(0,1)).toUpperCase();
NOTE: It's not tested so don't be suprised if that doesn't work.
•
•
Join Date: Jan 2006
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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
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.
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.
![]() |
Similar Threads
- program depending on String class (Java)
- String class (C++)
- C++ help (string class) (C++)
- get token from string using string class (C++)
- error in user defined string class (C++)
- String Class Of Java (Java)
Other Threads in the Java Forum
- Previous Thread: problem in installing jdbc...
- Next Thread: Reading in a .dat file from SSH shell command line
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation awt bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time tree webservices windows






