Probleme de string java.

deadalnix Messages postés 44 Statut Membre -  
 Tif -
Tout d'abors bonjour.

je suis un programmeur java ayant un peu d'experience mais la je me trouve confronté a un probleme de base. je vais vous expliquer ca en esperant que quelqu'un pourra m'eclairer . . .

Et bien voila, j'ai une string qui contient des données dans lequels peuvent etre incules des \' . Hors il me faut les tranformer en ' simple sans le \ . Ca parrit facile hein ?

j'ai essayé machaine.replaceAll("\\\'","\'") dans un premier temp mais ca n'as pas marché (rien ne se modifie).

Puis j'ai essayé machaine.replaceAll("\'","'") qui s'est tres bine compilé a mon grand etonnement mais qui ne modifiait pas la chaine non plus . . .

J'ai ensuite essayé un tas de variantes sans resultat. La j'ai vraiment besoin d'aide :P, alors si vous avez vaincu ce probleme faites moi signe ;).
A voir également:

2 réponses

Utilisateur anonyme
 
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
1
deadalnix Messages postés 44 Statut Membre 3
 
merci ;)
0
Tif
 
salut
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+
0
deadalnix Messages postés 44 Statut Membre 3
 
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 .
0