Results 1 to 11 of 11
  1. #1
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default 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.

    Java Code:
    public static String ConvertStringCaseFormat(String myString) {
    		myString = myString.substring(0, 1) + myString.substring(1, myString.length()).toLowerCase();
    		return myString;
    	} //end ConvertStringCaseFormat
    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.

    Thanks,

    David
    Last edited by DavidG24; 04-15-2011 at 07:11 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    If you are going to be doing plenty of String manipulations then look into the StringBuilder/StringBuffer classes.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Junky View Post
    If you are going to be doing plenty of String manipulations then look into the StringBuilder/StringBuffer classes.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    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

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by jasonwucinski View Post
    i would just use the String.toLowerCase() function to turn everything to lower case
    This creates a new String each time. If program needs to manipulate 5 million Strings your suggestion will end up with at least 10 million Strings. Potential memory leak.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Junky View Post
    This creates a new String each time. If program needs to manipulate 5 million Strings your suggestion will end up with at least 10 million Strings. Potential memory leak.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    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?


    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
    This would then eliminate the need to call multiple methods of the string class + the creation of extra un-necessary strings.

    Very interested in hearing what you think.

    Cheers,

    David
    Last edited by DavidG24; 04-15-2011 at 10:00 AM.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DavidG24 View Post
    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?


    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
    This would then eliminate the need to call multiple methods of the string class + the creation of extra un-necessary strings.

    Very interested in hearing what you think.

    Cheers,

    David
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    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 -

    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
    Thanks again for all your help,

    David

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DavidG24 View Post
    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 -

    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
    Thanks again for all your help,

    David
    Yep, that works fine, even if the String contains any non-letters (digits, symbols etc.) That is the way I should do it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

Similar Threads

  1. String to Hex Conversion
    By mocha in forum New To Java
    Replies: 0
    Last Post: 03-17-2010, 08:45 AM
  2. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM
  3. String Conversion....
    By hotice1027 in forum New To Java
    Replies: 8
    Last Post: 11-28-2008, 10:52 PM
  4. String to Object conversion
    By moaxjlou in forum New To Java
    Replies: 1
    Last Post: 10-29-2008, 01:04 AM
  5. string conversion??
    By j2vdk in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 03:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •