Results 1 to 7 of 7
  1. #1
    BennyJass is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Changing a String array into a String?

    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.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There may be a more efficient way, but I would create a String object set to "", then loop through concatenating each item in the array to my string object.

    Java Code:
    String s = "";

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Declare a String variable above a for loop, then loop through the array concatenating each item in the array into the String variable mentioned above. By concatenate I mean that you add each String held in the array to the summary String with the += operator. Note that if you have to do this a lot in your program, then you're better off using a SwingBuilder object instead a summary String.

  4. #4
    BennyJass is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    Hmm wait, so if I do something like:

    for (int counter = 0; counter < example.length; counter++) {
    whatIWant += example[counter];
    }
    That will combine all the strings?
    I completely forgot about the += operator.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by BennyJass View Post
    Hmm wait, so if I do something like:
    Better to try yourself and see what happens.

    I completely forgot about the += operator.
    This is nothing more than "syntactic sugar" and is not necessary to use as you can have the same result by calling:

    Java Code:
    newString = newString + arrayString[i]

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    A bit better: use a StringBuilder instead of concatenating Strings in a loop. Strings are immuatble so saying a+= b for two String values a and b, creates yet another String and forgets about the previous value of a. Read the API documentation of that class for the details.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    A bit better: use a StringBuilder instead of concatenating Strings in a loop. Strings are immuatble so saying a+= b for two String values a and b, creates yet another String and forgets about the previous value of a. Read the API documentation of that class for the details.
    Yep, we are in agreement as mentioned in post 3 above.

    I try not to push this too hard to newbies as in most of their small apps it smells too much of premature optimization. However if they're doing this sort of call repetitively in a critical portion of their code, then it is absolutely correct.

Similar Threads

  1. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  2. String to String Array
    By mitty in forum New To Java
    Replies: 2
    Last Post: 04-08-2010, 12:01 AM
  3. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  4. string to array
    By Drukqs in forum New To Java
    Replies: 5
    Last Post: 04-22-2009, 05:03 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •