|
|
|
|
Bon j'ai reussit a identifier le probleme : replaceAll demande un regex. N'y a-t-il pas un alternatif qui ne demande pas de regex, car je pense qu'au niveau resources, on peut faire mieux :p . |
Voici une petite classe que j'ai écrite et qui contient une méthode statique qui te permet de faire cela.
Regarde les tags Javadoc pour en comprendre le fonctionnenement. NB: la inner class ReplacerException est une exception qui est lancée si l'on recherche une chaîne plus longue que la longueur de la chaîne dans laquelle on la recherche.
public class StringReplacer {
/**
* Replaces a substring by another one in a String
*
* @param text The text where you want to replace a substring
* @param currentSubstring The substring to replace
* @param newSubstring The new substring
* @return The original text with all occurences of currentSubstring replaced by newSubstring
* @exception ReplacerException Exception which is thrown if the substring you ask to replace is longer than the whole text
* @author Philippe Fery
* @created June 04, 2002
*/
public static String replace(String text, String currentSubstring, String newSubstring) throws ReplacerException
{
int repeat = text.length() - currentSubstring.length();
if (repeat >= 0)
{
for (int i = 0; i < repeat; i++)
{
String testSubstring = text.substring(i, i + currentSubstring.length());
if (testSubstring.equals(currentSubstring))
{
text = text.substring(0, i) + newSubstring + text.substring(i + currentSubstring.length());
i += newSubstring.length();
}
}
}
else
{
throw replacerException;
}
return text;
}
/**
* ReplacerException is an Exception which is thrown if the substring you ask
* to replace is longer than the whole text.
*
* @author Philippe Fery
* @created June 04, 2002
*/
static class ReplacerException extends Exception
{
public String getMessage()
{
return "The substring to search is longer than the text";
}
}
private final static ReplacerException replacerException = new ReplacerException();
public static void main(String args[]){
try {
String str = StringReplacer.replace("abc\'def\'ghi\'\'jkl","\'","'");
System.out.println(str);
} catch (ReplacerException e) {
e.printStackTrace();
}
}
}
;-) HackTrack
|
Résultats pour probleme de string java.
Résultats pour probleme de string java.
Résultats pour probleme de string java.
Résultats pour probleme de string java.
Résultats pour probleme de string java.