1

I'm creating a game of hangman, and long story short, is there a shortcut of sorts to put this array of separate strings together into a string other than doing like below?

String after = under[0] + under[1] + under[2] + under[3] + under[4] + under[5] + under[6] + under[7] + under[8] + under[9] + under[10] + under[11] + under[12] + under[13] + under[14] + under[15] + under[16] + under[17] + under[18] + under[19] + under[20] + under[21];
5
  • 6
    ever heard of loops ? Commented Apr 4, 2013 at 13:53
  • If you can pull in an extra library, Apache Commons Lang has a StringUtils class that will let you do this String after = StringUtils.join(under, '');. Commented Apr 4, 2013 at 13:55
  • @Bartdude Hey, that's not nice... People do not hear about loops right away. I learned about loops when I got tired of typing the same code ten to fifteen times, changing a digit or two each time :):):) Commented Apr 4, 2013 at 14:04
  • @dasblinkenlight > Sorry if it seems rude... i'm maybe to new to SO but I didn't see it as an introduction to programming. I also don't find this question acceptable as we could discuss on which type of loop to use, not seeing the surrounding code of this line, and he doesn't describe a problem but ask on how to do this or that... Commented Apr 4, 2013 at 14:10
  • I have while loops and a few for loops throughout my code, mostly for guess-checking. I had never heard of StringBuilder as many people have suggested, or even thought of using the go-around boomz suggested on Strings. The code around it, I would think, is irrelevant, as I'm just looking for something less tedious. Commented Apr 4, 2013 at 14:20

6 Answers 6

2

You can do it with a loop, like this:

StringBuilder sb = new StringBuilder();
for (int i = 0 ; i != 22 ; i++) {
    sb.append(under[i]);
}
String after = sb.toString();

You can also add strings to an initially empty string, but that's suboptimal, because all the intermediate strings get allocated and released in a loop.

Sign up to request clarification or add additional context in comments.

Comments

1

Always use loop to perform repetitive operation like this.

Also, It is advised to use StringBuilder instead of String. String in JAVA is immutable.

StringBuilder sb = new StringBuilder();
int nCount = 22;

for (int iCnt = 0 ; iCnt <= nCount ; iCnt++) {
    sb.append(under[iCnt]);
}
String after = sb.toString();

Comments

0

try it:

String after = "";

for (int i=0; i<22; i++)
    after += under[i];

Comments

0

This code is not shorter, but is less tedious:

StringBuilder afterBldr = new StringBuilder();
for (String underEl : under) {
     afterBldr.append(underEl);
}
String after = afterBldr.toString();

Comments

0

Apache Joiner can be used here.

Joiner.on("").join(names)

Comments

0

If you have access to the Apache Commons library, use the join() method, it's the nicest solution:

String str = StringUtils.join(under, "");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.