Probleme de string java.
Fermé
deadalnix
Messages postés
44
Date d'inscription
dimanche 20 juin 2004
Statut
Membre
Dernière intervention
25 septembre 2006
-
19 juil. 2005 à 22:21
Tif - 8 mars 2007 à 15:54
Tif - 8 mars 2007 à 15:54
A voir également:
- Probleme de string java.
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel football - Télécharger - Jeux vidéo
- Java apk - Télécharger - Langages
- Waptrick java voiture - Télécharger - Jeux vidéo
- Java décompiler - Télécharger - Langages
2 réponses
Utilisateur anonyme
20 juil. 2005 à 08:40
20 juil. 2005 à 08:40
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.
;-)
HackTrack
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
deadalnix
Messages postés
44
Date d'inscription
dimanche 20 juin 2004
Statut
Membre
Dernière intervention
25 septembre 2006
3
19 juil. 2005 à 23:03
19 juil. 2005 à 23:03
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 .
20 juil. 2005 à 19:39
8 mars 2007 à 15:54
2 ans apres...
Tu avais tenté ça :
string.replaceAll("\\\\", "\\'")
car la gestiond slashs est assez complexe, il en faut 4 aligné pour en representer un dans un string, et en general 2 permettent d'identifier un caractere (surement pour ça que tu en avais tente 3, les deux premiers auraient du identifier le troisieme, mais non...)
a+