1 10 package org.mmbase.util; 11 12 import org.mmbase.util.logging.Logger; 13 import org.mmbase.util.logging.Logging; 14 15 27 public class Strip { 28 29 private static Logger log = Logging.getLoggerInstance(Strip.class.getName()); 31 32 35 public static final int NOTHING=0; 36 37 40 public static final int LEADING=1; 41 42 45 public static final int TRAILING=2; 46 47 50 public static final int BOTH=3; 51 52 59 public static String DoubleQuote(String str,int where) { 60 return Char(str,'"',where); 61 } 62 63 70 public static String SingleQuote(String str,int where) { 71 return Char(str,'\'',where); 72 } 73 74 82 public static String Whitespace(String str,int where) { 83 return Chars(str," \t\n\r",where); 84 } 85 86 94 public static String Char(String str,char chr,int where) { 95 if (str!=null && str.length()>0) { 96 int lead=0; 97 int trail=str.length()-1; 98 99 switch(where) { 100 case LEADING: 101 if (str.charAt(lead)==chr) lead++; 102 break; 103 case TRAILING: 104 if (str.charAt(trail)==chr) trail--; 105 break; 106 case BOTH: 107 if (str.charAt(lead)==chr) lead++; 108 if (str.charAt(trail)==chr) trail--; 109 break; 110 default: 111 break; 112 } 113 str=str.substring(lead,trail+1); 114 } 115 return str; 116 } 117 118 127 public static String Chars(String str,String chars,int where) { 128 129 if (str!=null && str.length()>0) { 130 int lead=0; 131 int trail=str.length()-1; 132 133 if (trail<1) { 134 where=LEADING; 135 } else { 136 switch(where) { 137 case LEADING: 138 while(chars.indexOf(str.charAt(lead))!=-1 && (lead<str.length()-1)) lead++; 139 break; 140 case TRAILING: 141 while(chars.lastIndexOf(str.charAt(trail))!=-1 && trail>0) trail--; 142 break; 143 case BOTH: 144 while(chars.indexOf(str.charAt(lead))!=-1 && lead<(str.length()-1)) lead++; 145 while(chars.lastIndexOf(str.charAt(trail))!=-1 && trail>=lead) trail--; 146 break; 147 default: 148 break; 149 } 150 } 151 if (lead<=trail) { 152 str=str.substring(lead,trail+1); 153 } else { 154 str=""; 155 } 156 } 157 return str; 158 } 159 160 163 public static void main(String args[]) { 164 log.info("Double "+Strip.DoubleQuote("\"double\"",Strip.BOTH)); 165 log.info("Single "+Strip.SingleQuote("'single'",Strip.BOTH)); 166 log.info("White |"+Strip.Whitespace(" white \n",Strip.BOTH)+"|"); 167 } 168 } 169 | Popular Tags |