问题描述:

Here's my code:

String abc = "abcdefghijklmnopqrstuvwxyz";

String [] abc1 = split(abc,b);

println (abc1);

...and for what it's worth here is what is printed:

[0] "a"

[1] "cdefghijklmnopqrstuvwxyz"

Is it possible for the 'b' to be included in either of the split strings? I'm not an experienced programmer so any help greatly appreciated as always.

网友答案:

I'm guessing you're looking for String's substring() and indexOf() methods instead:

String abc = "abcdefghijklmnopqrstuvwxyz";

println(abc.substring(0,abc.indexOf("b")));  //start to b
println(abc.substring(abc.indexOf("b")));    //b to end
网友答案:

Other option is to just add the 'b' back in. You know what your delimiter is, and you know where it was, so you can manually re-add it:

for (int i=0; i<abc.length; i++) {
    if (i > 0) {
        abc[i] = 'b' + abc[i] + 'b';
    }
}
abc[0] = abc[0] + 'b';
abc[abc.length-1] = 'b' + abc[abc.length-1];

It's more orthodox to go with substring() and indexOf(), though.

相关阅读:
Top