1 package snow.utils; 2 3 public final class StringUtils 4 { 5 6 public StringUtils() 7 { 8 9 } 10 11 14 public static String shortFormForDisplay(String 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 25 public static boolean startsWithIgnoresCaseAndBlanks(String text, String start) 26 { 27 for(int i=0; i<text.length()-start.length(); i++) 29 { 30 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 43 public static int indexOfStartTagIgnoreCase(String text, String tagName) 44 { 45 for(int i=0; i<text.length()-tagName.length(); i++) 46 { 47 if(text.charAt(i)=='<') 48 { 49 String tt = text.substring(i+1,i+1+tagName.length()); 50 if(tt.compareToIgnoreCase(tagName)==0) return i+1; 52 } 53 } 54 return -1; 55 } 56 57 60 public static int indexOfEndTagIgnoreCase(String text, String tagName) 61 { 62 for(int i=tagName.length(); i<text.length(); i++) 63 { 64 if(text.charAt(i)=='>') 65 { 66 String tt = text.substring(i-tagName.length(), i); 67 if(tt.compareToIgnoreCase(tagName)==0) return i; 69 } 70 } 71 return -1; 72 } 73 74 public static String 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 [] a) 87 { 88 } 89 90 } | Popular Tags |