• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Adding spaces to the front of a string

 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need some help, a hint, ... figuring out how to add spaces to the front of a string without creating a string of spaces and then concatenating the string of spaces to the string. I used the following code, but was nitpick for not modifying the string moveIt only.

Thanks in advance.
 
Ranch Hand
Posts: 111
jQuery Oracle C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I would do.
Take out the b entirely. While the length of moveIt is less than aNewLength change moveIt to equal a space plus itself.
After the while print the string.
Amber
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Amber. I had actually stumbled into the solution you gave, but didn't feel comfortable with it. With your comments I feel it is the correct one. Thanks again.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you consider using a different class. One that is very similar to String, but is mutable (i.e. can be changed).
Pete
 
Amber Woods
Ranch Hand
Posts: 111
jQuery Oracle C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What "different class" might that be, Pete?
Hint at least.
Amber
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pete, I have been reading about the String class in the JLS (4.3.3 The Class String 15.18.1). It looks like when a String is concatenated using the "+" a reference to the newly created String with the two operands concatenated is the result. So you end up with a new String obj. However, it also says that the compiler might implement StringBuffer (mutable) to optimize the string concatenation by reducing the number of intermediate Strings. Also in the API specs it says that for string concatenation operator(+) the StringBuffer and its append() method are implemented.
Would I be gaining anything by directly using a mutable class? It seems like I'm getting a mutable class by using the "+" on the String obj.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Richard, that is true, but it always turns it back into a String. So if you say this:
String a += b;
That JVM does something like this:
StringBuffer temp = new StringBuffer(a);
temp.append(b);
String a = temp.toString;
If you are only doing this once, it is not a big deal, I would just use a String, but if you have this in a loop, and have the potential for doing it many times, you will get better performance by using a StringBuffer and then after the loop, converting it back to a String.
Bill
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see said the blind man. Thanks for the input Bill, Pete and Amber. I have learned a lot today about strings.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic