-3

I have .txt files inside directory

I want to change the name of those .txt files, when saving.

I.e. /root/user/workspace/DataSet/dataset/file0.txt

I have already solved the problem in an inefficient way

for (int i = 0; i < img_n.length(); i++) {
    char a = img_n.charAt(i);

    if (a == '/') {
        c++;
    }

    if (c >= 6) {
        out += a;
    }
}
return out;

I knew the 6 times '/' will come so when c>=6 add char to the new string.

So This is NOT how to remove all '/' in an input string If you see my code clearly

It is also not taking the chars between '/'.

Therefore the Question is: You don't know how many times '/' comes but, you also want to remove the characters between '/'.

How can I do this more generic and efficient way?

Ahmet
  • 7,527
  • 3
  • 23
  • 47
  • 2
    Are you trying to turn `"/root/user/workspace/DataSet/dataset/file0.txt"` into `"file0.txt"`? – T.J. Crowder Dec 02 '16 at 13:12
  • On improvement would be to use a StringBuilder instead of out+= – JustinKSU Dec 02 '16 at 13:12
  • 1
    string.replace(toreplace, replacewith) – kmaork Dec 02 '16 at 13:12
  • you could use `img_n=img_n.replace("/","");` – XtremeBaumer Dec 02 '16 at 13:12
  • What is this code supposed to output? Can;t you just use... `String.replace("/", "")`? http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String) – BretC Dec 02 '16 at 13:13
  • @T.J.Crowder true, that's what I want – Ahmet Dec 02 '16 at 13:26
  • @AhmetTavli and waht if you have `"/root/user/workspace/DataSet/dataset/subfolder/file0.txt` ? should it output `file0.txt` or `subolder/file0.txt` – AxelH Dec 02 '16 at 13:31
  • @AxelH That's reason why I call my answer ' inefficient' – Ahmet Dec 02 '16 at 13:38
  • Owww, so the question is simply, **how to get the filename of a path ?** – AxelH Dec 02 '16 at 13:39
  • 1
    I don't think he is trying to be rude, but your question is confusing indeed. People have asked toy numerous times what you want as a result. You showed an example input string, but what kind of output do you expect? You say you indeed just want the file name as T.J. Crowder suggests, and yet from your following comments it doesn't seem like that is true. Therefore AlexH's wonderment is justified. What is the result you are trying to achieve? Give an example. – Bram Vanroy Dec 02 '16 at 14:41
  • Don't optimize prematurely. Try to make your code readable first like `String fileName = new File(fullPath).getName();`. – Pshemo Dec 02 '16 at 16:34
  • @Bram Vanroy you are right, I was nervous, sorry for my anxious reaction. I want to convert /root/user/workspace/src/dataset/file.txt into file.txt. so far Maurice Perry's answer solved my problem – Ahmet Dec 02 '16 at 17:06

1 Answers1

3

How about this:

int ix = img_n.lastIndexOf('/');
out = ix < 0 ? img_n : img_n.substring(ix+1);
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
  • The OP [has now confirmed](http://stackoverflow.com/questions/40933021/fastest-way-to-remove-all-characters-between-in-a-string?noredirect=1#comment69077537_40933021) this is what they're looking to do. – T.J. Crowder Dec 02 '16 at 13:29