Java String Array to String array2str(String[] arrayname, int length)

Here you can find the source of array2str(String[] arrayname, int length)

Description

arraystr

License

LGPL

Declaration

public static String array2str(String[] arrayname, int length) 

Method Source Code

//package com.java2s;
/**/*  ww w. ja v a2s  .  co m*/
 * Converts a line of text into an array of lower case words using a
 * BreakIterator.wordInstance(). <p>
 *
 * This method is under the Jive Open Source Software License and was
 * written by Mark Imbriaco.
 *
 * @param text a String of text to convert into an array of words
 * @return text broken up into an array of words.
 */

public class Main {
    public static String array2str(String[] arrayname, int length) {
        StringBuffer strbuf = new StringBuffer();
        for (int i = 0; i < length; i++) {
            strbuf.append("0");
        }
        for (int i = 0; i < arrayname.length; i++) {
            strbuf.replace(Integer.parseInt(arrayname[i]) - 1, Integer.parseInt(arrayname[i]), "1");
        }
        return strbuf.toString();
    }

    /**
     * Replaces all instances of oldString with newString in line.
     *
     * @param line the String to search to perform replacements on
     * @param oldString the String that should be replaced by newString
     * @param newString the String that will replace all instances of oldString
     *
     * @return a String will all instances of oldString replaced by newString
     */
    public static final String replace(String line, String oldString, String newString) {
        if (line == null) {
            return null;
        }
        int i = 0;
        if ((i = line.indexOf(oldString, i)) >= 0) {
            char[] line2 = line.toCharArray();
            char[] newString2 = newString.toCharArray();
            int oLength = oldString.length();
            StringBuffer buf = new StringBuffer(line2.length);
            buf.append(line2, 0, i).append(newString2);
            i += oLength;
            int j = i;
            while ((i = line.indexOf(oldString, i)) > 0) {
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
                j = i;
            }
            buf.append(line2, j, line2.length - j);
            return buf.toString();
        }
        return line;
    }

    /**
     * Replaces all instances of oldString with newString in line.
     * The count Integer is updated with number of replaces.
     *
     * @param line the String to search to perform replacements on
     * @param oldString the String that should be replaced by newString
     * @param newString the String that will replace all instances of oldString
     *
     * @return a String will all instances of oldString replaced by newString
     */
    public static final String replace(String line, String oldString, String newString, int[] count) {
        if (line == null) {
            return null;
        }
        int i = 0;
        if ((i = line.indexOf(oldString, i)) >= 0) {
            int counter = 0;
            counter++;
            char[] line2 = line.toCharArray();
            char[] newString2 = newString.toCharArray();
            int oLength = oldString.length();
            StringBuffer buf = new StringBuffer(line2.length);
            buf.append(line2, 0, i).append(newString2);
            i += oLength;
            int j = i;
            while ((i = line.indexOf(oldString, i)) > 0) {
                counter++;
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
                j = i;
            }
            buf.append(line2, j, line2.length - j);
            count[0] = counter;
            return buf.toString();
        }
        return line;
    }

    public static String replace(String oristr, String[] targetstr, String[] descstr) throws Exception {
        String result = null;
        try {
            if (targetstr.length == descstr.length) {
                for (int i = 0; i < targetstr.length; i++) {
                    oristr = oristr.substring(0, oristr.indexOf(targetstr[i])) + descstr[i]
                            + oristr.substring(targetstr[i].length() + 1);
                }
                result = oristr;
            }
        } catch (Exception e) {
            throw e;
        }
        return result;
    }

    public static int indexOf(CharSequence sb, char[] buff) {
        return indexOf(sb, buff, 0);
    }

    public static int indexOf(CharSequence sb, char[] buff, int fromIndex) {
        outer: for (int i = fromIndex; i <= sb.length() - buff.length; i++) {
            for (int j = 0; j < buff.length; j++) {
                if (sb.charAt(i + j) != buff[j]) {
                    continue outer;
                }
            }
            return i;
        }
        return -1;
    }
}

Related

  1. argsToString(String[] args, int start)
  2. argsToString(String[] args, int startFrom)
  3. arr2str(String[] arr)
  4. arr2str(String[] arr, String sep)
  5. array2str(String[] array, String delim)
  6. array2string(String array[])
  7. array2string(String[] s)
  8. arrayAsString(String[] haystack)
  9. arrayAsString(String[] stringArray)