1
votes

Pour séparer la sous-chaîne de la chaîne

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?


5 commentaires

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.


3 Réponses :


2
votes

Vous pouvez le faire avec

t=t.replace("clearpath","");


3 commentaires

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 ("/.*/","//");



0
votes

Cela fonctionne pour cette chaîne.

String t = "C:/clearpath/rafa.jpg";
t = t.substring(0, 3) + t.substring(12, t.length());


1 commentaires

J'ai posté une autre question, aidez-moi s'il vous plaît



0
votes

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();


1 commentaires

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