5
I have a string that uses parts of another string, how to make this code more "clean"? Since it is very difficult to understand.
String sessionHash = dirPath.substring(dirPath.substring(0, dirPath.length() - 1).lastIndexOf("/"));
sessionHash: /23980dc32e16792007de3343f1f99211/
dirPath: /home/Daniela/oknok/data/uploads/23980dc32e16792007de3343f1f99211/
+1 and if she wants the bars around (such as in the example) just add them via string concatenation.
– mgibsonbr
@mgibsonbr changed to resolve this although I doubt that it is the intention to keep the bars. I was doing the same as you but I had an abortion because I didn’t think it would make me feel any better.
– Maniero
Apart from the
substring
double, does not improve much even... You would know if this proposed method, using theFile
, will give an equal result on a Windows system? (which uses "" instead of "/" as separator) I think will, but I’m without compiler here to test...– mgibsonbr
the intention was to keep the bars, thank you. Really much more readable and easy to maintain.
– Daniela Morais
@mgibsonbr as far as I know will work, but also can not guarantee.
– Maniero
If I’m not mistaken, there’s a constant in the class
File
, toFileSeparator
, which is best for this. Using the response value gets a little stuck to the OS for code execution.– mutlei
@mutlei I agree but in this case will not generate a file name but a string that the author only where will use. From what I understood need to be so even independent from OS, therefore in this specific case should not be used the
separator
. Unless she says it has to be different.– Maniero