从路径中删除最后一个文件夹

时间:2016-04-19 16:46:09

标签: javascript jquery

var loc = window.location.pathname;
var dir = loc.substring(0, loc.lastIndexOf('/'));

loc返回为:

/public/html/signup/

dir返回:

/public/html/signup

我想删除文件夹的名称,所以我回来了:

/public/html/

我做错了什么?谢谢!

2 个答案:

答案 0 :(得分:2)

你走了。它是一个通用的解决方案,因此您可以根据需要删除任意数量的文件夹 - 只需传递除1以外的其他内容。

public static int LCS(String A, String B, int m, int n) {
    int table[][] = new int[m + 1][n + 1];

    for (int i = 0; i < m; i++) {
        table[i][0] = 0;
    }
    for (int i = 1; i < n; i++) {
        table[0][n] = 0;
    }
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (A.charAt(i) == B.charAt(j)) {
                table[i][j] = table[i - 1][j - 1] + 1;
            } else {
                table[i][j] = max(table[i][j - 1], table[i - 1][j]);
            }
        }
    }

    return table[m][n];
}

private static int max(int a, int b) {
    return (a > b) ? a : b;
}

public static void main(String args[]) {
    Scanner in = new Scanner(System.in);

    System.out.println("Your input words:\n");
    String x = in.nextLine();
    String y = in.nextLine();

    in.close();

    int m = x.length();
    int n = y.length();

    System.out.println("Length of LCS is " + LCS(x, y, m, n));
}

答案 1 :(得分:0)

我知道只有一种JavaScript本机方法可以删除数组元素并返回此修改列表的深层副本。我也可以提及reduce,但这不在这里。

const new_path = path.split('/').filter((basename, index, array) => index !== array.length - (basename === '' ? 2 : 1)).join('/');

过滤器很酷,因为它可以帮助我们对回调中的空基名进行排序,这相当于本例中路径中的随即斜杠。

相关问题