java - Trim Filename to be short enough -
i writing api based gui, in want full filename appear... filename long, , want trim filename leave out few folders needed. example:
c:\users\roiex\somefolder\someotherfolder\somefile.someextension
should trimmed like
c:\...\someotherfolder\somefile.someextension
i have variable filename, generated calling file.getabsolutepath()
. have method, let's call toolarge(filename)
start :
string filename = file.getabsolutepath(); while(toolarge(filename)){ filename = trimpath(filename); } render(filename);
what should inside trimpath()
?
any appreciated
a proper regex replaceall()
work :
public static void main(string[] args) { string s = "c:\\asas\\users\\roiex\\somefolder\\someotherfolder\\somefile.someextension"; system.out.println(s.replaceall("(\\w+:\\\\).*?(\\\\\\w+\\\\\\w+.\\w+)$", "$1...$2")); }
o/p :
c:\...\someotherfolder\somefile.someextension
the above regex selects last starting directory , last directory file name.
Comments
Post a Comment