Create String from String Array [Java]


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Create String from String Array [Java]

  1. #1
    Join Date
    Feb 2005
    Posts
    24

    Question Create String from String Array [Java]

    -- Coding in NetBeans IDE 4.0 Beta2 [JAVA] --

    Given a String Array [saMessage] of unknown size, I need to create a String [sMessage] (so I get do a .GetBytes and send it over a Socket) that has “~” as a delimiter between each field. For example:

    saMessage[] = [“Home”, “0001”, “UNSPECIFICED”, “192.168.0.0”]
    I need this String[] array (this one has 4 fields) to be converted to the following String (including the delimiter “~” between each field so I can re-create it at the other end of my Socket)
    sMessage = Home~0001~UNSPECIFIED~192.168.0.0~
    (or something similar with the delimiters, just so that at the other end I am able to reconstruct my String Array give the string)

    This makes it easier for me to manipulate each section of the string (when in the string array form I can do like saMessage[0] = “Other”, etc…), and I need to put it in a string to do the .GetBytes function). Any ideas? (I tried playing with the String and String[] classes and found nothing that could help, do I need to import something?

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Can't you use a StringBuffer ?
    Like:
    Code:
    StringBuffer sb=new StringBuffer();
    for (int i=0; i<stringArray.length; i++) {
      if (i > 0) sb.append("~");
      sb.append(stringArray[i]);
    }
    byte [] b=sb.toString().getBytes();
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center