Share this page 

Convert a String to an array or an array to a stringTag(s): Language


String to Array
NOTE: This HowTo was designed for JDK 1.0.2. Starting with version 1.4, the String class offers a better way to split a String into an Array. It's the String.split(regexp) method. See this HowTo.

public String[] stringtoArray( String s, String sep ) {
  // convert a String s to an Array, the elements
  // are delimited by sep
  // NOTE : for old JDK only (<1.4).
  //        for JDK 1.4 +, use String.split() instead
  StringBuffer buf = new StringBuffer(s);
  int arraysize = 1;
  for ( int i = 0; i < buf.length(); i++ ) {
    if ( sep.indexOf(buf.charAt(i) ) != -1 )
      arraysize++;
  }
  String [] elements  = new String [arraysize];
  int y,z = 0;
  if ( buf.toString().indexOf(sep) != -1 ) {
    while (  buf.length() > 0 ) {
      if ( buf.toString().indexOf(sep) != -1 ) {
        y =  buf.toString().indexOf(sep);
        if ( y != buf.toString().lastIndexOf(sep) ) {
          elements[z] = buf.toString().substring(0, y );
          z++;
          buf.delete(0, y + 1);
        }
        else if ( buf.toString().lastIndexOf(sep) == y ) {
          elements[z] = buf.toString().substring
            (0, buf.toString().indexOf(sep));
          z++;
          buf.delete(0, buf.toString().indexOf(sep) + 1);
          elements[z] = buf.toString();z++;
          buf.delete(0, buf.length() );
        }
      }
    }
  }
  else {
    elements[0] = buf.toString();
  }
  buf = null;
  return elements;
}
Thanks to T. GUIRADO
Array to String
The StringBuffer approach :
public class StringUtils {
  private StringUtils() {}
  public static String arrayToString(String[] a, String separator) {
    if (a == null || separator == null) {
        return null;
    }
    StringBuffer result = new StringBuffer();
    if (a.length > 0) {
        result.append(a[0]);
        for (int i=1; i < a.length; i++) {
            result.append(separator);
            result.append(a[i]);
        }
    }
    return result.toString();
  }

  public static void main(String args[]) throws Exception {
    System.out.println(
         StringUtils.arrayToString(new String[] { "a" , "b", "c" } ,  ",")
         );
  }
}
The StringBuilder approach (JDK1.5):
public class StringUtils {
  private StringUtils() {}
  public static String arrayToString(String[] a, String separator) {
    if (a == null || separator == null) {
        return null;
    }
    StringBuilder result = new StringBuilder();
    if (a.length > 0) {
        result.append(a[0]);
        for (int i=1; i < a.length; i++) {
            result.append(separator);
            result.append(a[i]);
        }
    }
    return result.toString();
  }

  public static void main(String args[]) throws Exception {
    System.out.println(
         StringUtils.arrayToString(new String[] { "a" , "b", "c" } ,  ",")
         );
  }
}
Where possible, it is recommended that StringBuilder be used in preference to StringBuffer as it will be faster under most implementations.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.