1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
| package org.essilab.exercices.bases.string;
import org.essilab.exercices.bases.string.interfaces.IStringUtil;
import org.essilab.exercices.bases.string.interfaces.IStringUtil.IReplacementCallback;
public abstract class StringUtil implements IStringUtil {
private String String retour;
/**
* <p>Suppress invisible character placed before of after the given string</p>
* <p>for example : " a test " will return "a test"</p>
* @param string the string to trim
* @return the trimed string.
*/
public String trim(String string){
string = string.trim();
return string;
}
/**
* <p>Turn to lowercase the given string.</p>
* <p>for example : "AdCvD,de" will return "adcvd,de"
* </p><p>you can not use Character.is* method</p>
* @param string lowercased string
* @return
*/
public String toLowercase(String string){
String a = new String();
a = string.toLowerCase();
return a ;
}
/**
* <p>Turn to uppercase the given string.</p>
* <p>for example : "AdCvD,de" will return "ADCVD,DE" </p>
* <p>you can not use Character.is* method</p>
* @param string uppercased string
* @return
*/
public String toUppercase(String string){
String b = new String();
b = string.toUpperCase();
return b;
}
/**
* <p>Capitalize the given string</p>
* <p>for example, "aDc bbb,ddd?e:fgh" will return "Adc Bbb,Ddd?E:Fgh"
* </p><p>you can use Character.is* method</p>
* @param string the string to capitalize
* @return
*/
public String capitalize (String string) {
boolean flag;
String charSpecials = ";\":? ,";
StringBuffer buffer = new StringBuffer("");
for (int i = 0; i < string.length(); i++) {
flag = false;
buffer.append(string.charAt(i));
for (int j = 0; j < charSpecials.length(); j++) {
if (string.charAt(i) == charSpecials.charAt(j)) {
flag = true;
break;
}
}
if (flag == true) {
if (i < string.length() - 1) {
i++;
}
buffer.append(Character.toUpperCase(string.charAt(i)));
}
}
buffer.deleteCharAt(buffer.length() - 1);
String retour = buffer.toString();
return retour;
}
/**
* <p>return the camelized upper representation of the given string</p>
* <p>for example, "abg Def.ghi, bla" will return AbaDefGhiBla</p>
* @param string
* @return
*/
public String camelizeUpper(String string){
boolean flag;
String charSpecials = ";\":? ,";
StringBuffer buffer = new StringBuffer("");
for (int i = 0; i < string.length(); i++) {
flag = false;
buffer.append(string.charAt(i));
for (int j = 0; j < charSpecials.length(); j++) {
if (string.charAt(i) == charSpecials.charAt(j)) {
flag = true;
break;
}
}
if (flag == true) {
if (i < string.length() - 1) {
i++;
}
buffer.append(Character.toUpperCase(string.charAt(i)));
}
}
buffer.deleteCharAt(buffer.length() - 1);
String retour = buffer.toString();
String rendu = retour.replaceAll("[\\s\\p{Punct}]","");
return rendu;
}
/**
* <p>return the camelized lower representation of the given string</p>
* <p>for example, "aba Def.ghi, bla" will return abaDefGhiBla</p>
* @param string
* @return
*/
public String camelizeLower(String string){
boolean flag;
String charSpecials = ";\":? ,";
StringBuffer buffer = new StringBuffer("");
for (int i = 0; i < string.length(); i++) {
flag = false;
buffer.append(string.charAt(i));
for (int j = 0; j < charSpecials.length(); j++) {
if (string.charAt(i) == charSpecials.charAt(j)) {
flag = true;
break;
}
}
if (flag == true) {
if (i < string.length() - 1) {
i++;
}
buffer.append(Character.toUpperCase(string.charAt(i)));
}
}
buffer.deleteCharAt(buffer.length() - 1);
String retour = buffer.toString();
//suppression de toute ponctuation
String rendu = retour.replaceAll("[\\s\\p{Punct}]","");
String nouveau= rendu.substring(0,1).toLowerCase();
return nouveau;
}
/**
* <p>Revert the given string</p>
* <p>for example, "abc" will return "cba"
* @param string
* @return
*/
public String revert(String string){
StringBuilder lettersBuff = new StringBuilder(string);
String inverse = lettersBuff.reverse().toString();
return inverse ;
}
/**
* </p><p>Replace in the given input the founded patterns by the replacement.</p>
* <p>You can't use any replace method or any regexp</p>
* <p>for example</p>
* <p>"hello world, hello" with pattern "hello" and replacement "goodbye" will produce</p>
* <p>goodbye world, goodbye</p>
* @param input to input to analyse
* @param pattern the pattern to look for.
* @param replacement the replacement to apply for the matched occurence
* @param all applied replacement to all occurence ?
* @return
*/
/*
* public String replace(String input, String pattern, String replacement, boolean all){
if (StringUtils.isNotBlank(input)){
if (all){
return input.replaceAll(pattern, replacement);
}else{
return input.replaceFirst(pattern, replacement);
}
}
return null;
}*/
/*public String replace(String input, String pattern, String replacement, boolean all){
all = false;
int tab [] = new int [6];
int a = 0;
if(input.contains(pattern)){
for(int i =0; i<6;i++){
a = input.indexOf(pattern, a+1);
tab[i] = a;
}
all = true;
}
if (all == true){
for(int j = 0; j<6;j++){
int e = 0;
if(input.indexOf(a + 1) == tab[e]))
}
}
}
*/
/**
* <p>Search for pattern in the given string and call the callback for each replacement</p>
* <p>You can't use any replace method or any regexp</p>
* <p>for example</p>
* <pre> * public class HrefReplacement implements IReplacementCallback {
* public String replace(String pattern) {
* return "<a href=\"/tags/"+pattern+".html\">"+pattern+"";
* }
* }
* </pre>
* <p>will produce for given string : "hello world" with given pattern "world" : hello <a href="http://10.50.126.57:8181/tags/hello.html">world&lgt;/a>
* @param input to input to analyse
* @param pattern the pattern to look for.
* @param replacement the replacement to apply for the matched occurence
* @param all applied replacement to all occurence ?
* @return
*/
//public String replace(String input, String pattern, IReplacementCallback callback, boolean all);
public interface IReplacementCallback{
public String replace(String pattern);
}
} |
Partager