StringUtil.java :  » Game » foogamelib » kku » cs » util » Java Open Source

Java Open Source » Game » foogamelib 
foogamelib » kku » cs » util » StringUtil.java
package kku.cs.util;

import java.util.Vector;

public class StringUtil {
  static public String[] split(String src, String delimeter) {
    Vector<String> v = new Vector<String>();
    int p1 = 0;
    int p2 = 0;

    while ((p2 = src.indexOf(delimeter, p1)) >= p1) {
      v.add(src.substring(p1, p2).trim());
      p1 = p2 + 1;
    }
    if (p1 < src.length()) {
      v.add(src.substring(p1).trim());
    }
    return v.toArray(new String[v.size()]);
  }

  static public boolean inarray(int x, int a[]) {
    for (int i = 0; i < a.length; i++) {
      if (a[i] == x)
        return true;
    }
    return false;
  }

  static public String[] split(String src, int... delimeter) {
    Vector<String> v = new Vector<String>();
    char[] data = src.toCharArray();
    String s = "";
    for (int i = 0; i < data.length; i++) {
      if (data[i] == '"') {
        i++;
        while (data[i] != '"' && i < data.length) {
          s = s + data[i];
          i++;
        }
        continue;
      }
      if (data[i] == '\'') {
        i++;
        while (data[i] != '\'' && i < data.length) {
          s = s + data[i];
          i++;
        }
        continue;
      }

      if (inarray(data[i], delimeter)) {
        v.add(s);
        s = "";
      } else {
        s = s + data[i];
      }
    }
    v.add(s);
    return v.toArray(new String[v.size()]);
  }

  static public void main(String[] a) {

    String v[] = split("('xx,xx',2,3)", ',', '(', ')');
    for (String s : v) {
      System.out.println(s);
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.