• Post Reply Bookmark Topic Watch Topic
  • New Topic

Turn an String array all into one String?  RSS feed

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok lets say I have a string array like so:

String[] example = {"Poopy", "55","coolio");

How could I change this, all into one String like so:

String whatIWant = "Poopy55coolio";

I appreciate it.
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with an empty whatIWant String (""). Using a for loop, iterate through each of the Strings in your array and add them to whatIWant. Please respond if you need more advice.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I was thinking that, but if I did this

for (int counter = 0; counter < example.length; counter++) {
whatIWant = example[counter];
}

Doesn't it just change the example every time until it reaches the ending length.
So the outcome would be:
whatIWant = "coolio";

Oh wait...would I do this:
whatIWant += example[counter];
?
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try

whatIWant += example[counter];
 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try toString method of java.util.Arrays class.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't concatenate the Strings but use a StringBuilder.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!