AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sostituzione, nella stringa, di " con "

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java (Italian)
View previous topic :: View next topic  
Author Message
Infiliset
Guest





PostPosted: Wed May 24, 2006 7:07 pm    Post subject: Sostituzione, nella stringa, di " con \" Reply with quote



Ciao a tutti, ho un semplice problema che non riesco a risolvere. Devo
riuscire a sostituire, ad una stringa, il carattere " con i caratteri \", e
il carattere \ con il carattere \\.

Cerco di spiegarmi meglio: nella stringa "prova" ho un testo letto da file,
che contiene " e \. Io devo, in qualche modo, sostituire e mettere al posto
di " -> \" e al posto di \ -> \\. Ma come?
Io ho provato a fare:

prova = prova.replaceAll("\"", \\\"); // non ottengo il risultato sperato
prova = prova.replaceAll("\\", "\\\\"); // mi solleva un'eccezione

Spero di essere stato chiaro. Grazie in anticipo a chi mi risponderà!

Distinti saluti

Matteo
Back to top
Guest






PostPosted: Thu May 25, 2006 10:57 am    Post subject: Re: Sostituzione, nella stringa, di " con \" Reply with quote



String a = "ciao\" come\" \\ \\ va?";
a = a.replace("\\","\\\\");
a = a.replace("\"","\\\"");
System.out.println(a);
Back to top
Caesar
Guest





PostPosted: Thu May 25, 2006 11:08 am    Post subject: Re: Sostituzione, nella stringa, di " con \" Reply with quote



infiliset (AT) gmail (DOT) com ha scritto:
Quote:
iomcenter-giginus (AT) yahoo (DOT) it ha scritto:

String a = "ciao\" come\" \\ \\ va?";
a = a.replace("\\","\\\\");
a = a.replace("\"","\\\"");
System.out.println(a);

Anzitutto grazie per avermi risposto! Ho provato questo codice, ma mi
da doppio errore: method replace(java.lang.String, java.lang.String)
not found. Se può essere utile, sto utilizzando Java 5. Come mai?

Al limite, fallo "a mano" senza il replaceAll:

for (i = 0; i < dataFilePath.length(); i++)
if (dataFilePath.charAt(i)=='\\') {
dataFilePath = dataFilePath.substring(0, i) + '\\' +
dataFilePath.substring(i);
i++;
}


--
Ave!
Caesar.

Eatvr quo nos deorvm ostenta vocant. Alea iacta est!
Caivs Ivlivs Caesar, Imperator Mvndi
CaesarOdiaLoSpam (AT) Fastwebnet (DOT) It
Back to top
Guest






PostPosted: Thu May 25, 2006 11:08 am    Post subject: Re: Sostituzione, nella stringa, di " con \" Reply with quote

oppure usando le regular expression

String a = "ciao\" come\" \\ \\ va?";
String regEx = "\\\\";
a = a.replaceAll(regEx,"\\\\\\\\");
regEx = "\\\"";
a = a.replaceAll(regEx,"\\\\\\\"");
System.out.println(a);
Back to top
Guest






PostPosted: Thu May 25, 2006 11:08 am    Post subject: Re: Sostituzione, nella stringa, di " con \" Reply with quote

java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)
a me compila, il metodo replace e' questo:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replace(java.lang.CharSequence,%20java.lang.CharSequence)
Back to top
Guest






PostPosted: Thu May 25, 2006 11:08 am    Post subject: Re: Sostituzione, nella stringa, di " con \" Reply with quote

iomcenter-giginus (AT) yahoo (DOT) it ha scritto:

Quote:
String a = "ciao\" come\" \\ \\ va?";
a = a.replace("\\","\\\\");
a = a.replace("\"","\\\"");
System.out.println(a);

Anzitutto grazie per avermi risposto! Ho provato questo codice, ma mi
da doppio errore: method replace(java.lang.String, java.lang.String)
not found. Se può essere utile, sto utilizzando Java 5. Come mai?
Grazie!


Distinti saluti

Matteo
Back to top
Infiliset
Guest





PostPosted: Thu May 25, 2006 2:07 pm    Post subject: Re: Sostituzione, nella stringa, di " con \" Reply with quote

"Caesar" <CaesarOdiaLoSpam (AT) Fastwebnet (DOT) It> ha scritto nel messaggio
news:nqfdg.1255$y01.224 (AT) tornado (DOT) fastwebnet.it...
Quote:

Al limite, fallo "a mano" senza il replaceAll:

for (i = 0; i < dataFilePath.length(); i++)
if (dataFilePath.charAt(i)=='\\') {
dataFilePath = dataFilePath.substring(0, i) + '\\' +
dataFilePath.substring(i);
i++;
}


Grazie a tutti per avermi risposto! Sono riuscito a risolvere il problema,
facendo come diceva iomcenter-giginus: utilizzando la funzione replace.
Grazie ancora!


Distinti saluti

Matteo
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java (Italian) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.