Here you can find the source of arrayToString(String[] array)
This takes a String[] and returns a String that can be converted back into an identical String[] using the stringToArray method.
Parameter | Description |
---|---|
array | the String[] to convert into a String. |
public static String arrayToString(String[] array)
//package com.java2s; // License as published by the Free Software Foundation; either // public class Main { /**/* ww w. j a v a2 s. c om*/ * <p> * This takes a String[] and returns a String that can be converted * back into an identical String[] using the stringToArray method. * </p> * <p> * The intended purpose of this is to possibly aid in implementing the * getStringForm() method of the abstract NodeAddressID class. * </p> * @param array the String[] to convert into a String. * @return the converted array as a String. */ public static String arrayToString(String[] array) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < array.length; i++) { sb.append(stuffString(array[i])); if (i < array.length - 1) sb.append("\"\\\""); } return sb.toString(); } private static String stuffString(String str) { StringBuffer sb = new StringBuffer(str); int charsInserted = 0; int foundIndex1 = str.indexOf("\"", 0); int foundIndex2 = str.indexOf("\\", 0); int foundIndex = -1; if (foundIndex1 > -1 && foundIndex1 < foundIndex2) foundIndex = foundIndex1; else foundIndex = foundIndex2; while (foundIndex > -1) { sb.insert(foundIndex + charsInserted, "\\"); charsInserted = charsInserted + 1; foundIndex1 = str.indexOf("\"", ++foundIndex); foundIndex2 = str.indexOf("\\", foundIndex); if (foundIndex1 > -1 && (foundIndex1 < foundIndex2 || foundIndex2 == -1)) foundIndex = foundIndex1; else foundIndex = foundIndex2; } return sb.toString(); } }