Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help ApendingStrings  RSS feed

 
Steven Smith
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a very simple example:
Want to get this "#abc#def#ghi#jkl" from this "abcdefghijkl"
This will work:
String str = "abcdefghijkl";
String tmpStr = "";
String newStr = "";
for (int i = 0; i < str.length(); i = i+3){
tmpStr = str.substring(i, i + 3);
newStr = newStr + "#" + tmpStr;
}

Is 'newStr = newStr + "#" + tmpStr;' efficient?
What's a better way if str is huge?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the StringBuilder and StringBuffer classes?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, yes, Or you could just use:

If you're using a JDK earlier than 1.4, or if you're not yet comfortable with regular expressions, you will probably want to use a StringBuffer instead.
[ August 20, 2006: Message edited by: Jim Yingst ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, I appreciate irony. :roll:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking back at the original post, this deserves a more direct answer:

[Steven]: Is 'newStr = newStr + "#" + tmpStr;' efficient?

Not very. For small strings or a small number of iterations through the loop, it's probably fine. But if the string is very large, this can be quite inefficient. The key problem is that every single time this line is executed, the existing contents of newStr are copied into a new String. Actually they're probably copied into a new StringBuffer or StringBuilder, and then recopied into a new String. Anyway it's wasteful if you repeat the process many times.

[Steven]: What's a better way if str is huge?

Better to have a single StringBuffer or StringBuilder which you copy everything to once, then call toString() just once to convert the content to a String. You can see this embedded within a portion of Tony's code. Something similar is called indirectly by the replaceAll() call in my code. But here's a simple version that more closely resembles the code you originally used:

The main differences are:
  • The String variable newStr is replaced with a StringBuffer sb. This allows all the new content to accumulate in one place, with a minimum of needless recopying. (Recopying does sometimes occur within the StringBuffer or StringBuilder class, but it's been reasonably well-optimized so it doesn't affect performance nearly as much as the original code did.)
  • The tempStr variable has been eliminated as unnecessary. (No effect on performance, it just seems simpler to me.)
  • I added one more character ("l") to the input string, and intoduced the "end" variable to more elegantly handle a string whose length is not an exact multiple of 3. If you try adding "l" in your original code, you'll get an exception, which is probably not what you want.

  • If you're using JDK 1.5+, you can replace the StringBuffer with StringBuilder.
    [ August 20, 2006: Message edited by: Jim Yingst ]
     
    Tony Morris
    Ranch Hand
    Posts: 1608
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    ...
    here's a simple version that more closely resembles the code you originally used:
    ...

    I'd like to correct this statement. The given version is not simpler, just less verbose (ala Java). It is actually many times more complex with many more relationships among the code that is exposed to the client. Verbosity is an unfortunate consequence of simplicity inherent within the language.
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Eh, I'll leave it to the reader to determine that for themselves. My main objective the second post was to provide something with recognizable connections to what the poster was already thinking about. This is Beginner after all. Maybe we could just focus on what the poster actually asked about, rather than your usual laundry list of gripes and moans about the Java language?
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!