分割字符串但保留第一个分隔符

时间:2021-03-23 22:08:31

I have a string in the form "/A/B/C/D". Is there a regex or a simple way to capture this into a String array of the form [/A, B, C, D]? Essentially, split on "/" but retain the first instance of the delimiter? I can be guaranteed that the input string will have "/" as the first character and at least one "/" following the first. My attempt has been this so far:

我有一个字符串在表格“/ a /B/C/D”。是否有regex或简单的方法将其捕获到表单的字符串数组[/ a, B, C, D]?本质上,在“/”上拆分,但保留分隔符的第一个实例?我可以保证输入字符串的第一个字符是"/",第一个字符后面至少有一个"/"。到目前为止,我的尝试是:

private String[] customSplit(String input) {
    if (!input.startsWith("/")) {
        input = "/" + input;
    }

    String[] output = input.split("/");
    output[1] = "/" + output[1];
    return output;
}

The above is a bit clunky though (and has an empty "" spot at index 0) so any suggestions?

上面的内容有点笨拙(索引0处有一个空的“”点),有什么建议吗?

3 个解决方案

#1


2  

You can use a negative lookahead in split to avoid split on first delimiter:

您可以在split时使用一个负面的前视,以避免在第一个分隔符上进行分割:

String[] toks = "/A/B/C/D".split("(?!^)/");
//=> [/A, B, C, D]

Here (?!^) is a negative lookahead that will skip start position for delimiter /

在这里(? ! ^)是一个消极的超前,将跳过开始为分隔符/位置

RegEx Demo

RegEx演示

#2


0  

A dirty solution..but at least should work. It assumes that you have at least 2 elements in the string, otherwise you've to add some error handling code, but from what you've written it should always be like that.

一个肮脏的解决方案。但至少应该奏效。它假设在字符串中至少有2个元素,否则必须添加一些错误处理代码,但是从您编写的内容来看,它应该是这样的。

private String[] customSplit(String input) {
    String preparedInput=input;
    if (input.startsWith("/")) {
        preparedInput=input.substring(1);
    }

    String[] output = preparedInput.split("/");
    output[0] = "/" + output[0];
    return output;
}

#3


0  

Another way is:

另一种方法是:

private String[] customSplit(String input) {
    String[] output = input.substring(1).split("/");
    output[0] = "/" + output[0];
    return output;
}

#1


2  

You can use a negative lookahead in split to avoid split on first delimiter:

您可以在split时使用一个负面的前视,以避免在第一个分隔符上进行分割:

String[] toks = "/A/B/C/D".split("(?!^)/");
//=> [/A, B, C, D]

Here (?!^) is a negative lookahead that will skip start position for delimiter /

在这里(? ! ^)是一个消极的超前,将跳过开始为分隔符/位置

RegEx Demo

RegEx演示

#2


0  

A dirty solution..but at least should work. It assumes that you have at least 2 elements in the string, otherwise you've to add some error handling code, but from what you've written it should always be like that.

一个肮脏的解决方案。但至少应该奏效。它假设在字符串中至少有2个元素,否则必须添加一些错误处理代码,但是从您编写的内容来看,它应该是这样的。

private String[] customSplit(String input) {
    String preparedInput=input;
    if (input.startsWith("/")) {
        preparedInput=input.substring(1);
    }

    String[] output = preparedInput.split("/");
    output[0] = "/" + output[0];
    return output;
}

#3


0  

Another way is:

另一种方法是:

private String[] customSplit(String input) {
    String[] output = input.substring(1).split("/");
    output[0] = "/" + output[0];
    return output;
}