package yi.util.data;

import java.util.*;

public class StringUtil {

  /**
   * Replace part of the string
   * @param original
   * @param to_be_replaced
   * @param replace_with
   * @return
   */
  public static String replace(String original, String to_be_replaced, String replace_with) {
    int index = original.indexOf(to_be_replaced);
    if (index < 0) return original;
    return original.substring(0, index) + replace_with + original.substring(index + to_be_replaced.length());
  }

  public static String removeBoundaryBlanks(String str) {
    int begin_index = 0;
    while (begin_index < str.length()) {
      char c = str.charAt(begin_index);
      if (!Character.isWhitespace(c)) break;
      begin_index ++;
    }
    int end_index = str.length() - 1;
    while (end_index >= 0) {
      char c = str.charAt(end_index);
      if (!Character.isWhitespace(c)) break;
      end_index --;
    }
    if (begin_index >= str.length() || end_index < 0) return new String("");
    return str.substring(begin_index, end_index + 1);
  }

  public static String[] getDeliminatedStrings(String str, String deliminators, boolean removeBlank) {
    StringTokenizer tokenizer = new StringTokenizer(str, deliminators);
    String[] deliminated = new String[tokenizer.countTokens()];
    int count = 0;
    while (tokenizer.hasMoreTokens()) {
      String s = tokenizer.nextToken();
      if (removeBlank) {
        s = StringUtil.removeBoundaryBlanks(s);
      }
      if (s.length() > 0) {
        deliminated[count++] = s;
      }
    }

    if (removeBlank) {
      String[] removed = new String[count];
      for (int i=0; i<count; i++) removed[i] = deliminated[i];
      deliminated = removed;
    }
    return deliminated;

  }

  public static String getClassShortName (String full_name)
  {
      int last_dot= full_name.lastIndexOf(".");
      if (last_dot==-1) return full_name;
      return full_name.substring(last_dot+1);
  }

  public static String getFileSuffixNameRemoved(String name) {
    int lastdot = name.lastIndexOf('.');
    if (lastdot >= 0) return name.substring(0, lastdot);
    return name;
  }

  /**
   *
   */
  public static void main(String[] args) {
    String a = " ".trim();
    System.out.println(a.length());
  }
}