This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Turn an String array all into one String? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Turn an String array all into one String?" Watch "Turn an String array all into one String?" New topic
Author

Turn an String array all into one String?

Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
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.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
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

Joined: Sep 25, 2010
Posts: 76
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];
?
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 530
Try

whatIWant += example[counter];


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2928
    
  15

You can try toString method of java.util.Arrays class.


Mohamed Sanaulla | My Blog
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Don't concatenate the Strings but use a StringBuilder.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Turn an String array all into one String?
 
Similar Threads
Strign of arrays
"length" method ambiguity..
How is priority determined?-Collection Doubt
String handling.
Casting Object arrays to String arrays