如何截断字符串(包含路径)而不触及文件名(保留最后一个文件夹,文件名和驱动器号)?
您好, 我正在寻找一种截断路径以适应指定宽度的方法。
我已经在SO上搜索了,我发现了这个: Truncate a string nicely to fit within a given pixel width
但是它在字符串的末尾添加了......我想有一个类似的功能,但它需要保持开始和结束..
例如,我有这条路径(字符串):
H:\Informatique\Personnalisation\Icones\__\Crystal GT PNG Pack\Applications\licq.png
它需要像这样适合div的宽度:
H:\Informatique\...PNG Pack\Applications\licq.png
另一个例子:
D:\A___VERY___________LONG________PATH____________\myfile.png
将缩短为:
D:\A___VERY___________LONG________PA...\myfile.png
最后一例:
D:\A___VERY___________LONG________PATH____________\and-a-sub-solder\myfile.png
将缩短为:
D:\A___VERY________...\and-a-sub-solder\myfile.png
限制:
长文件名例如:
D:\my____file___________________________name____is___too____________long.png
将缩短为:
D:\my____file_________..._is___too____________long.png
我看到窗户和苹果这样做的方式很好......但我找不到附近的任何剧本。
任何带有类似脚本的链接,以便我可以尝试调整它? 或者任何想帮助我的人? :)
非常感谢
答案 0 :(得分:4)
这是一个有趣的问题,这是我的解决方案:http://jsfiddle.net/AlienHoboken/y7SgA/
它假设你有一个带有路径的变量str,以及带有你的最大字符长度的maxLength。
您需要稍微更改代码,因为str是硬编码的,您需要更改maxLength,并且您还需要更改写入信息的位置(它现在只是写入正文)。
注意:如果您的驱动器名称+文件名超过maxLength变量,它将只打印出这两个变量,即使它们太大了。
代码,因为你不喜欢jsfiddle:
$(document).ready(function () {
var str = "H:\\Informatique\\Personnalisation\\Icones\\__\\Crystal GT PNG Pack\\Applications\\licq.png";
var maxLength = 25;
var tokens = str.split("\\");
var drive = tokens[0];
var fileName = tokens[tokens.length - 1];
var len = drive.length + fileName.length;
//remove the current lenth and also space for 3 dots and 2 slashes
var remLen = maxLength - len - 5;
//if remLen < 0, then it will over flow our maxLength to still maintain drive and filename
if (remLen > 0) {
//remove first and last elements from the array
tokens.splice(0, 1);
tokens.splice(tokens.length - 1, 1);
//recreate our path
var path = tokens.join("\\");
//handle the case of an odd length
var lenA = Math.ceil(remLen / 2);
var lenB = Math.floor(remLen / 2);
//rebuild the path from beginning and end
var pathA = path.substring(0, lenA);
var pathB = path.substring(path.length - lenB);
path = drive + "\\" + pathA + "..." + pathB + "\\" + fileName;
//write it out
$("body").html("Orig. Path: " + str + "<br /><br />" +
"New Path: " + path + "<br /><br />" +
"MaxLength: " + maxLength + "<br /><br />" +
"New Length: " + path.length);
} else {
//try and fit our maxlength by taking only drive and filename
$("body").html("Orig. Path: " + str + "<br /><br />" +
"New Path: " + drive + "\\" + fileName + "<br /><br />" +
"MaxLength: " + maxLength + "<br /><br />" +
"New Length: " + (len + 1) + "<br /><br />");
}
});
答案 1 :(得分:2)
让我添加@AlienHoboken代码的优化版本。这个版本
希望它有所帮助!
function pathShorten(str, maxLength, removeFilename) {
var splitter = str.indexOf('/')>-1 ? '/' : "\\",
tokens = str.split(splitter),
removeFilename = !!removeFilename,
maxLength = maxLength || 25,
drive = str.indexOf(':')>-1 ? tokens[0] : "",
fileName = tokens[tokens.length - 1],
len = removeFilename ? drive.length : drive.length + fileName.length,
remLen = maxLength - len - 5, // remove the current lenth and also space for 3 dots and 2 slashes
path, lenA, lenB, pathA, pathB;
//remove first and last elements from the array
tokens.splice(0, 1);
tokens.splice(tokens.length - 1, 1);
//recreate our path
path = tokens.join(splitter);
//handle the case of an odd length
lenA = Math.ceil(remLen / 2);
lenB = Math.floor(remLen / 2);
//rebuild the path from beginning and end
pathA = path.substring(0, lenA);
pathB = path.substring(path.length - lenB);
path = drive + splitter + pathA + "..." + pathB + splitter ;
path = path + (removeFilename ? "" : fileName);
//console.log(tokens, maxLength, drive, fileName, len, remLen, pathA, pathB);
return path;
}