J'ai cette chaîne dans la variable java en tant que
String j = "C://rafa.jpg".
Maintenant, je veux supprimer clearpath
de la variable t
et je veux stocker dans autre variable comme
String t = "C:/clearpath/rafa.jpg";
Comment suis-je censé faire cela?
3 Réponses :
Vous pouvez le faire avec
t=t.replace("clearpath","");
String.remove ()
n'existe pas.
Je vais essayer ça, merci.
Il est également possible d'utiliser une regex: t = t.replaceAll (regex, "");
par exemple t = t.replaceAll ("/.*/","//");
Cela fonctionne pour cette chaîne.
String t = "C:/clearpath/rafa.jpg"; t = t.substring(0, 3) + t.substring(12, t.length());
J'ai posté une autre question, aidez-moi s'il vous plaît
Vous pouvez utiliser java.nio.file.Path
.
En utilisant Chemin
, vous pouvez manipuler votre chemin de plusieurs manières.
Dans votre cas, vous pouvez obtenir le nom du fichier par
C:\rafa.jpg
& file root en tant que
System.out.println(fileRoot.toString() + fileNm.toString());
À la fin, concaténez-les simplement.
Path fileRoot = path.getRoot();
La sortie finale sera comme p >
Path path = Paths.get("C:/clearpath/rafa.jpg"); Path fileNm = path.getFileName();
Ajoutez File.separatorChar
lors de la concaténation en tant que fileRoot.toString () + File.separatorChar + fileNm.toString ()
pour obtenir une sortie sous la forme C: \\ rafa .jpg
clearpath est un nom constant?
Non, ce n'est pas constant
Dans ce cas, vous devez expliquer plus précisément la règle du chemin à décaper
RTFM peut-être?
String path = "clearpath"; Chaîne j = t.replace (chemin, "");
@StefanBecker merci, je vais essayer.