我需要一些关于字符串操作的建议。这是一个可能的字符串:
"/branches/projects/MyTool/MyTool/someOtherFolderOrFile"
我需要的是一个包含以下内容的数组:
['/branches', '/branches/projects','/branches/projects/MyTool','/branches/projects/MyTool/MyTool']
这样澄清一下这个数组应包含的内容:指向最后一个文件或文件夹的所有父路径。最后一个文件或文件夹在数组中排除。
我知道我可以将字符串拆分为"/"
,然后可以迭代数组,连接字符串并删除数组中的最后一个条目。这个方法我会递归调用,直到数组中没有元素为止。然后,每次迭代都会将连接的字符串保存到一个新数组中,该数组最终会保存所需的结果......
但是我问我怎么做得非常干净简单,我相信你们中的某个人能想出一个极好的解决方案:)
修改 这是我刚才提出的当前解决方案:
var string = "/branches/projects/MyTool/MyTool/anotherFolderOrFile";
var helperArr = string.split("/");
var endArray = [];
getParentArray();
console.log(endArray);
function getParentArray() {
if(helperArr.length == 1) {
return;
}
else {
var helperString = "";
for(var i = 0;i<helperArr.length;i++) {
helperString = helperString+helperArr[i];
if(i!= helperArr.length -1) {
helperString = helperString+"/";
}
}
endArray.push(helperString);
helperArr.length = helperArr.length-1;
getParentArray();
}
}
答案 0 :(得分:4)
这是一个紧凑的解决方案,但它可能不像@somethinghere那样清晰:
var path= '/branches/projects/MyTool/MyTool/someOtherFolderOrFile'.split('/'),
paths= [];
path.shift();
while(path.length > 1) {
paths.push((paths[paths.length-1] || '') + '/' + path.shift());
}
alert(paths.join('\r'));
&#13;
<强>更新强>
感谢您的评论。这是一个更紧凑的版本,不需要分割或移位:
var path= '/branches/projects/MyTool/MyTool/someOtherFolderOrFile',
paths= [],
i = 1;
while(i = path.indexOf('/',i)+1) {
paths.push(path.substr(0,i));
}
alert(paths.join('\r'));
&#13;
更新#2
只是为了咯咯笑,第三个解决方案,应该比我的其他解决方案更清晰,但still a little slower比@something here:
var path= '/branches/projects/MyTool/MyTool/someOtherFolderOrFile',
paths= [],
i;
for(var i = 1 ; i < path.length ; i++) {
if(path[i]==='/') paths.push(path.substr(0,i));
}
alert(paths.join('\r'));
&#13;
答案 1 :(得分:1)
这样可以解决问题:
/* First split the path at any folders separator ('/')*/
var splitPath = "/branches/projects/MyTool/MyTool/someOtherFolderOrFile".split("/");
/* Initialise a final array to save the paths. Store the first one in there! */
var endPaths = [splitPath[0] + "/"];
/* Check if there are multiple paths available */
if(splitPath.length >= 2){
/* Run through each and append it to the last saved one, then add it to the results.*/
for(var i = 1; i <= splitPath.length-1; i++){
endPaths.push(endPaths[i-1] + splitPath[i] + "/")
}
}
这将包括/
作为路径,这在技术上是一条路径。如果您不想要,可以在初始化时使用此行来排除任何类似的简单路径:
var endPaths = splitPath[0] != "" ? [splitPath[0] + "/"] : [];
如果您执行上述操作,请注意,当循环开始运行时,您的循环将抛出错误endPaths[i-1]
将为undefined
,您必须在for循环中进行检查:< / p>
/* You need to do -2 as you start at 1, but you only want to add paths from 2 upwards.*/
var prevPath = typeof endPaths[i-2] !== "undefined" ? endPaths[i-2] : "";
endPaths.push(prevPath + splitPath[i] + "/")
答案 2 :(得分:1)
而且,对于更多种类,基于RegExp的递归解决方案:
function iterateMatch(testString, currPattern, results) {
var regexTestPattern = new RegExp("^" + currPattern + "\/[^\/]+");
results.push(testString.match(regexTestPattern)[0]);
if (results[results.length - 1] !== testString) {
iterateMatch(testString, results[results.length - 1], results)
}
}
该功能接受:
&#34;忽略&#34; pattern将以空白字符串开头,但在每次迭代期间,将包含先前匹配的目录级别。此字符串用作匹配模式的开头,然后在结尾添加一个RegExp模式,其中包含一个RegExp模式以匹配正斜杠和一个或多个非斜杠字符。
匹配在结果数组中捕获,如果它不等于测试字符串,则会再次传递到iterateMatch
函数,因为下一个&#34;忽略&#34; pattern(连同原始测试字符串和结果数组),以便匹配开始查看下一级别。
当像这样调用时,使用测试字符串:
var sTestString = "/branches/projects/MyTool/MyTool/someOtherFolderOrFile";
var sCurrPattern = "";
var aResults = [];
iterateMatch(sTestString, sCurrPattern, aResults);
console.log(aResults);
。 。 。结果是:
["/branches",
"/branches/projects",
"/branches/projects/MyTool",
"/branches/projects/MyTool/MyTool",
"/branches/projects/MyTool/MyTool/someOtherFolderOrFile"]
答案 3 :(得分:0)
这是我的看法。使用一点点jQuery来帮助我们创建一个非常简洁的函数(即使它有点效率低下)。
function getParentArray(path) {
// split up on the forward slashes
var splitPath = path.split('/');
// Don't need the last part of the path
splitPath.pop();
// put an ever growing slice of the original path into a new array
return $.map(splitPath, function(value, i) {
return '/' + splitPath.slice(1, i + 1).join('/');
}).splice(1);
}
var samplePath = '/branches/projects/MyTool/MyTool/someOtherFolderOrFile';
var parentArray = getParentArray(samplePath);
alert(parentArray.join('\n'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 4 :(得分:0)
一点都不短,一点也不优雅......有点不同的方法......
function split_string_to_path( str ) {
if (typeof str == "undefined")
return false; // return, if no string
split_path = str.split("/"); // splitted string
result_arr = []; // result paths into this variable
tmp_arr = []; // temporary, for join
for( i = 0; i < split_path.length; i++ ) { // loop through all elements
tmp_arr.push( split_path[i] ); // push element
result_path = tmp_arr.join( "/" );
if ( result_path.length > 0 )
result_arr.push( tmp_arr.join( "/" ) ); // join tmp arr to path and add it to result_arr
}
return result_arr;
}
split_result = split_string_to_path( "/branches/projects/MyTool/MyTool/someOtherFolderOrFile" ); // run function
console.log( split_result ); // output on console
答案 5 :(得分:0)
尝试
var str = "/branches/projects/MyTool/MyTool/someOtherFolderOrFile"
, d = "/"
, arr = str.split(d).filter(Boolean)
, res = arr.map(function(val, i) {
return i < 1 ? d + val : d + arr.slice(0, i).join(d) + d + arr[i]
}).slice(0, arr.length - 1);
var str = "/branches/projects/MyTool/MyTool/someOtherFolderOrFile"
, d = "/"
, arr = str.split(d).filter(Boolean)
, res = arr.map(function(val, i) {
return i < 1 ? d + val : d + arr.slice(0, i).join(d) + d + arr[i]
}).slice(0, arr.length - 1);
document.querySelectorAll("pre")[0].innerText = JSON.stringify(res, null, 4)
&#13;
<pre></pre>
&#13;
答案 6 :(得分:0)
此x = benchmark_returns.index
y = benchmark_returns['Crypto 30']
y2 = benchmark_returns['Dow Jones 30']
y3 = benchmark_returns['NASDAQ']
y4 = benchmark_returns['S&P 500']
fig, ax = plt.subplots()
line, = ax.plot(x, y, color='k')
line2, = ax.plot(x, y2, color = 'b')
line3, = ax.plot(x, y3, color = 'r')
line4, = ax.plot(x, y4, color = 'g')
def update(num, x, y, y2, y3, y4, line):
line.set_data(x[:num], y[:num])
line2.set_data(x[:num], y2[:num])
line3.set_data(x[:num], y3[:num])
line4.set_data(x[:num], y4[:num])
return line, line2, line3, line4,
ani = animation.FuncAnimation(fig, update, fargs=[x, y, y2, y3, y4, line],
interval = 1, blit = True)
plt.show()
解决了我的用例(没有前面的select * from sys.dm_os_waiting_tasks
):
reduce
如果您绝对是疯子,也可以选择这种单线:
/