KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > utils > StringUtils


1 package SnowMailClient.utils;
2
3 public class StringUtils
4 {
5
6   public StringUtils()
7   {
8
9   } // Constructor
10

11
12
13   /** used for example to detect html files (starting with <html>)
14     ignores the blanks at the text start
15   */

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

34   public static int indexOfStartTagIgnoreCase(String JavaDoc text, String JavaDoc tagName)
35   {
36     for(int i=0; i<text.length()-tagName.length(); i++)
37     {
38       if(text.charAt(i)=='<')
39       {
40          String JavaDoc tt = text.substring(i+1,i+1+tagName.length());
41          //System.out.println("."+tt+".");
42
if(tt.compareToIgnoreCase(tagName)==0) return i+1;
43       }
44     }
45     return -1;
46   }
47
48   /** finds the index of tagName>
49      @return the position of >
50   */

51   public static int indexOfEndTagIgnoreCase(String JavaDoc text, String JavaDoc tagName)
52   {
53     for(int i=tagName.length(); i<text.length(); i++)
54     {
55       if(text.charAt(i)=='>')
56       {
57          String JavaDoc tt = text.substring(i-tagName.length(), i);
58          //System.out.println("."+tt+".");
59
if(tt.compareToIgnoreCase(tagName)==0) return i;
60       }
61     }
62     return -1;
63   }
64
65
66   public static String JavaDoc toString(byte[] array)
67   {
68     if(array==null) return "null";
69     if(array.length==0) return "{}";
70     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("{");
71     for(byte b:array)
72     {
73       sb.append(""+b);
74       sb.append(", ");
75
76     }
77     return sb.substring(0, sb.length()-2)+"}";
78   }
79
80   public static void main(String JavaDoc[] a)
81   {
82     System.out.println(indexOfStartTagIgnoreCase("<br> \r\t\n <br><p> <html>", "bR"));
83   }
84
85 } // StringUtils
Popular Tags