Example #1
0
  public static String replace(String text, String[] from, String[] to, String keys) {
    // keys - is first chars of from
    StringBuilder result = new StringBuilder();
    int pos = 0;
    while (pos < text.length()) {
      char ch = text.charAt(pos);

      int index = keys.indexOf(ch);
      while (-1 != index) {
        if (text.startsWith(from[index], pos)) {
          pos += from[index].length();
          result.append(to[index]);
          break;
        }
        index = keys.indexOf(text.charAt(pos), index + 1);
      }

      if (-1 == index) {
        result.append(ch);
        pos++;
      }
    }

    return result.toString();
  }
Example #2
0
  public static String urlEscape(String param) {
    String urlOK = "";
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < param.length(); ++i) {
      char ch = param.charAt(i);
      char lowerCh = Character.toLowerCase(ch);
      if (Character.isDigit(ch) || (-1 != "[email protected]".indexOf(lowerCh))) {
        sb.append(ch);

      } else if (' ' == ch) {
        sb.append('+');

      } else if ((0x7F & ch) == ch) {
        putCh(sb, ch);

      } else if ((0xFFF & ch) == ch) {
        putCh(sb, 0xD0 | (ch >> 6));
        putCh(sb, 0x80 | (0x3F & ch));

      } else {
        putCh(sb, 0xE8 | (ch >> 12));
        putCh(sb, 0x80 | (0x3F & (ch >> 6)));
        putCh(sb, 0x80 | (0x3F & ch));
      }
    }
    return sb.toString();
  }
Example #3
0
 public static String implode(String[] text, String separator) {
   StringBuilder result = new StringBuilder();
   for (String item : text) {
     if (null != item) {
       if (0 != result.length()) {
         result.append(separator);
       }
       result.append(item);
     }
   }
   return result.toString();
 }
Example #4
0
 public static String replace(String text, String from, String to) {
   int fromSize = from.length();
   int start = 0;
   int pos = 0;
   StringBuilder sb = new StringBuilder();
   for (; ; ) {
     pos = text.indexOf(from, pos);
     if (-1 == pos) break;
     sb.append(text.substring(start, pos)).append(to);
     pos += fromSize;
     start = pos;
   }
   if (start < text.length()) {
     sb.append(text.substring(start));
   }
   return sb.toString();
 }
Example #5
0
  public static String longitudeToString(long seconds) {
    int days = (int) (seconds / 86400);
    seconds %= 86400;
    int hours = (int) (seconds / 3600);
    seconds %= 3600;
    int minutes = (int) (seconds / 60);

    StringBuilder buf = new StringBuilder();
    if (days != 0) {
      buf.append(days).append(' ').append(JLocale.getString("days")).append(' ');
    }
    if (hours != 0) {
      buf.append(hours).append(' ').append(JLocale.getString("hours")).append(' ');
    }
    if (minutes != 0) {
      buf.append(minutes).append(' ').append(JLocale.getString("minutes"));
    }

    return buf.toString();
  }
Example #6
0
  public static String getLocalDateString(long gmtDate, boolean onlyTime) {
    if (0 == gmtDate) return "***error***";
    int[] localDate = createDate(gmtTimeToLocalTime(gmtDate));

    StringBuilder sb = new StringBuilder(16);

    if (!onlyTime) {
      sb.append(Util.makeTwo(localDate[TIME_DAY]))
          .append('.')
          .append(Util.makeTwo(localDate[TIME_MON]))
          .append('.')
          .append(localDate[TIME_YEAR])
          .append(' ');
    }

    sb.append(Util.makeTwo(localDate[TIME_HOUR]))
        .append(':')
        .append(Util.makeTwo(localDate[TIME_MINUTE]));

    return sb.toString();
  }
Example #7
0
 private static void putCh(StringBuilder sb, int ch) {
   String s = Integer.toHexString(ch);
   sb.append("%");
   if (1 == s.length()) sb.append('0');
   sb.append(s);
 }