java - StringBuilder improperly reversing a filename -
i have stringbuilder used reverse , return string. string in question filename: "server_1.8.2_java_8.jar" (this vary).
when reverse string , return it, becomes "sre_.._aa8jra._vj281rve". i've used variations of (as regular sentences "reverse string") , same result.
however, if directly assign string inside method it's reversed in (rather taking parameter , performing some operations on it), seems work properly.
test method code in case helps:
static string parsejarfilename(string jarlocation) { if(jarlocation == null) return ""; // path jar location empty, // return stuff doesn't messed string jarfilename = ""; // name of .jar server executable for(int = jarlocation.length(); jarlocation.charat(i - 1) != '/'; --) { // read string backwards until / found, // signaling jarfile name has ended // done, concatenate characters of jarfile name jarfilename += jarlocation.charat(i - 1); jarfilename.trim(); // jarfile name backwards here, reverse string str = new stringbuilder(jarfilename).reverse().tostring(); jarfilename = str; } return jarfilename; } public static void main(string[] args) { string filepath = "/users/john/desktop/server/server_1.8.2_java_8.jar"; string filename = parsejarfilename(filepath); system.out.println(filename); } // written 2xedo: twitter.com/2xedo this code prints "sre_.._aa8jra._vj281rve" console.
if not mistaken looking like
public static string parsejarfilename(string jarlocation) { if (jarlocation == null) return ""; return jarlocation.substring(jarlocation.lastindexof('/')+1); } or maybe little more readable
public static string parsejarfilename(string jarlocation) { if (jarlocation == null) return ""; return new file(jarlocation).getname(); } you can use path newer (and in cases preferred) version of file
return paths.get(jarlocation).getfilename().tostring();
Comments
Post a Comment