001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.util;
007:
008: import java.util.*;
009:
010: /**
011: * Miscellaneous utilities
012: */
013: public class String2Array {
014:
015: /**
016: * Convert a string to an array of strings based on a delimiter character.
017: * @param s a string to be converted into an array
018: * @param delim delimiters char used to break up the string
019: * @param trim whether to trim() the resulting strings
020: * @return the converted array
021: */
022: static public String[] string2Array(String s, char delim,
023: boolean trim) {
024: if (s.length() == 0)
025: return new String[] {};
026: List a = new ArrayList();
027: char c;
028: int start = 0, end = 0, len = s.length();
029: for (; end < len; ++end) {
030: c = s.charAt(end);
031: if (c == delim) {
032: String p = s.substring(start, end);
033: a.add(trim ? p.trim() : p);
034: start = end + 1;
035: }
036: }
037: // grab the last element
038: String p = s.substring(start, end);
039: a.add(trim ? p.trim() : p);
040: return (String[]) a.toArray(new String[a.size()]);
041: }
042:
043: /**
044: * Convert a string to an array of strings based on a delimiter character.
045: * The returned strings are not trimmed.
046: * @param s a string to be converted into an array
047: * @param delim delimiters used to break up the string
048: * @return the converted array
049: */
050: static public String[] string2Array(String s, char delim) {
051: return string2Array(s, delim, false);
052: }
053:
054: /**
055: * Convert an array of strings to a single string separated by the delimiter chars.
056: * @param a array to be converted into a string
057: * @param delim delimiters used to break up the string
058: * @return the converted string
059: */
060: static public String array2String(String[] a, String delim) {
061: StringBuffer sb = new StringBuffer();
062: for (int i = 0; i < a.length; ++i) {
063: sb.append(a[i]);
064: if (i < a.length - 1)
065: sb.append(delim);
066: }
067: return sb.toString();
068: }
069:
070: public static void main(String[] args) {
071: System.out.println(array2String(new String[] { "blah",
072: " blah ", " blah" }, " :: "));
073: String s;
074: char delim;
075: String[] a;
076:
077: s = ",,blah, blah ,, , , blah , ,,";
078: delim = ',';
079: System.out.println("[" + s + " -> " + delim + "]");
080: a = string2Array(s, delim);
081: for (int i = 0; i < a.length; ++i) {
082: System.out.println("<" + a[i] + ">");
083: }
084: System.out.println("[" + s + " -> " + delim + "]");
085: a = string2Array(s, delim, true);
086: for (int i = 0; i < a.length; ++i) {
087: System.out.println("<" + a[i] + ">");
088: }
089: s = "x,y,z";
090: System.out.println("[" + s + " -> " + delim + "]");
091: a = string2Array(s, delim);
092: for (int i = 0; i < a.length; ++i) {
093: System.out.println("<" + a[i] + ">");
094: }
095: s = ",";
096: System.out.println("[" + s + " -> " + delim + "]");
097: a = string2Array(s, delim);
098: for (int i = 0; i < a.length; ++i) {
099: System.out.println("<" + a[i] + ">");
100: }
101: s = "";
102: System.out.println("[" + s + " -> " + delim + "]");
103: a = string2Array(s, delim);
104: for (int i = 0; i < a.length; ++i) {
105: System.out.println("<" + a[i] + ">");
106: }
107: }
108:
109: }
|