KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > StringUtils


1 package snow.utils;
2
3 public final class StringUtils
4 {
5
6   public StringUtils()
7   {
8
9   }
10
11   /** Long strings are replaced with "start...end"
12   * @param maxLen should be approx 70
13   */

14   public static String JavaDoc shortFormForDisplay(String JavaDoc str, int maxLen)
15   {
16      if(str==null) return "ERROR: null string in shortFormForDisplay";
17      if(str.length()<maxLen) return str;
18      int mk = Math.max(5, maxLen/2-5);
19      return str.substring(0,mk)+" .... "+str.substring( str.length()-mk, str.length());
20   }
21
22   /** used for example to detect html files (starting with <html>)
23     ignores the blanks at the text start
24   */

25   public static boolean startsWithIgnoresCaseAndBlanks(String JavaDoc text, String JavaDoc start)
26   {
27     // Can be boost with Knuth's algorithm for fast search... ###
28
for(int i=0; i<text.length()-start.length(); i++)
29     {
30        //System.out.println("\""+text.substring(i, i+start.length())+"\"");
31
if( text.substring(i, i+start.length()).equalsIgnoreCase(start)) return true;
32
33        if( text.charAt(i)==' ' || text.charAt(i)=='\r' || text.charAt(i)=='\n'
34            || text.charAt(i)=='\t') continue;
35        break;
36     }
37     return false;
38   }
39
40   /** finds the index of <tagName
41      @return the position of the first letter of the tag, not <
42   */

43   public static int indexOfStartTagIgnoreCase(String JavaDoc text, String JavaDoc tagName)
44   {
45     for(int i=0; i<text.length()-tagName.length(); i++)
46     {
47       if(text.charAt(i)=='<')
48       {
49          String JavaDoc tt = text.substring(i+1,i+1+tagName.length());
50          //System.out.println("."+tt+".");
51
if(tt.compareToIgnoreCase(tagName)==0) return i+1;
52       }
53     }
54     return -1;
55   }
56
57   /** finds the index of tagName>
58      @return the position of >
59   */

60   public static int indexOfEndTagIgnoreCase(String JavaDoc text, String JavaDoc tagName)
61   {
62     for(int i=tagName.length(); i<text.length(); i++)
63     {
64       if(text.charAt(i)=='>')
65       {
66          String JavaDoc tt = text.substring(i-tagName.length(), i);
67          //System.out.println("."+tt+".");
68
if(tt.compareToIgnoreCase(tagName)==0) return i;
69       }
70     }
71     return -1;
72   }
73
74   public static String JavaDoc formatTime(long millis)
75   {
76      if(millis<0) return "-" + formatTime(-millis);
77
78      if(millis<1000) return millis+" ms";
79      int sec = (int) millis/1000;
80      if(millis<60000) return millis/1000 + " s";
81      int min = sec/60;
82      sec = sec-min*60;
83      return min+" m "+sec+" s";
84   }
85
86   public static void main(String JavaDoc[] a)
87   {
88   }
89
90 } // StringUtils
Popular Tags