Results 1 to 11 of 11
Thread: String conversion function
- 04-15-2011, 07:09 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 49
- Rep Power
- 0
String conversion function
Hi guys,
I want to be able to take a string in Uppercase, i.e. HELLO and convert it so that the first letter is in Uppercase and the rest lowercase, i.e.
HELLO --> Hello
I've written a simple method (taking advantage of the methods in the String class), i.e.
This will be operated on a good 10^6 Strings and was hoping to get peoples input regarding how effecient / inefficent the above method is - and if there are suggestions for faster methods - please let me know.Java Code:public static String ConvertStringCaseFormat(String myString) { myString = myString.substring(0, 1) + myString.substring(1, myString.length()).toLowerCase(); return myString; } //end ConvertStringCaseFormat
Thanks,
DavidLast edited by DavidG24; 04-15-2011 at 07:11 AM.
- 04-15-2011, 07:12 AM #2
If you are going to be doing plenty of String manipulations then look into the StringBuilder/StringBuffer classes.
- 04-15-2011, 07:18 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Maybe in this particular case a conversion to a char array (String.toCharArray()) and manipulation of the individual chars (using the methods in the Character class) and a single conversion back to a String again is most efficient. Testing and timing it will tell the answer ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-15-2011, 07:27 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
i would just use the String.toLowerCase() function to turn everything to lower case, then use the String.replace() function and String.toUpperCase() function to replace the first char of the string. if there's more than one word that needs to have its case changed, use a for loop and text delimeter to iterate through the entire stirng
- 04-15-2011, 07:36 AM #5
- 04-15-2011, 07:50 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
That wouldn't be so bad by itself but the first character of the String has to be an uppercase character, so that implies another String creation for the concatenation of two Strings ... The OP's solution needs:
1) a new String for the first character;
2) a new String for the second and next characters;
3) a new String for the lowercase version of 2)
4) a new String for the concatenation of 1) and 3)
Using a simple char array is a bit more efficient (as I wrote above).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-15-2011, 09:58 AM #7
Member
- Join Date
- Apr 2009
- Posts
- 49
- Rep Power
- 0
First off, thank you to all that have responded.
To Josah, strings are effectively stored as char arrays in memory correct? if would be nice if we could point directly to it in memory (circa c++ style). Nevertheless would the following be a more efficient method?
This would then eliminate the need to call multiple methods of the string class + the creation of extra un-necessary strings.Java Code:public static String ConvertStringCaseFormat(String myString) { final int CONVERSION_DIFFERENCE = 32; char[] myStringchars = myString.toCharArray(); for(int i = 1 ; i < myStringchars.length; i++) { myStringchars[i] += CONVERSION_DIFFERENCE; } //end for return new String(myStringchars); } //end ConvertStringCaseFormat
Very interested in hearing what you think.
Cheers,
DavidLast edited by DavidG24; 04-15-2011 at 10:00 AM.
- 04-15-2011, 10:34 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Yep, that was the way I was suggesting; it 'only' takes the creation of a char array and the creation of another String; I would do the lowercase conversion by calling a Character utility method (not the entire world is ASCII you know). Strings are immutable which is fine in general but it can cause a bit of overhead when you want to use them as if they were simple char buffers or arrays. But then again the C/C++ way causes a lot of bugs ;-)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-15-2011, 10:54 AM #9
Member
- Join Date
- Apr 2009
- Posts
- 49
- Rep Power
- 0
Hi Jos,
I promise this will be the last time I bug you, in reference to what you said regarding the Char conversion, if I impose that I will always only pass strings that are composed of letters then the previous code should be -
Thanks again for all your help,Java Code:public static String ConvertStringCaseFormat(String myString) { char[] myStringchars = myString.toCharArray(); for(int letter = 1 ; letter < myStringchars.length; letter++) { myStringchars[letter] = Character.toLowerCase(myStringchars[letter]); } //end for return new String(myStringchars); } //end ConvertStringCaseFormat
David
- 04-15-2011, 11:25 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
- 04-15-2011, 11:41 AM #11
Member
- Join Date
- Apr 2009
- Posts
- 49
- Rep Power
- 0
Similar Threads
-
String to Hex Conversion
By mocha in forum New To JavaReplies: 0Last Post: 03-17-2010, 08:45 AM -
combine string[] into string like perl's join function
By tekberg in forum Advanced JavaReplies: 9Last Post: 02-23-2009, 01:05 PM -
String Conversion....
By hotice1027 in forum New To JavaReplies: 8Last Post: 11-28-2008, 10:52 PM -
String to Object conversion
By moaxjlou in forum New To JavaReplies: 1Last Post: 10-29-2008, 01:04 AM -
string conversion??
By j2vdk in forum New To JavaReplies: 13Last Post: 09-19-2008, 03:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks