开发者

Split byte array with string?

开发者 https://www.devze.com 2023-03-14 12:27 出处:网络
I\'d like to achieve something like this: String sentence = \"Hi there over there\"; String[]result = sentence.split(\"there\");

I'd like to achieve something like this:

String sentence = "Hi there over there";
String[]result = sentence.split("there");
//you get result[0] = Hi, result[1] = over

Is it possible to split using the String i开发者_如何学编程n a byte array format?

byte[]delimiter = "there".getBytes();
byte[]byteSentence = sentence.getBytes();
//then somehow split byteSentence using delimiter.


You can, of course, convert the byte arrays into strings:

byte[] delimiter = "test".getBytes();
byte[] sentence = "this is a test sentence".getBytes();

String[] result = new String(sentence).split(new String(delimiter));
byte[][] resultByte = new byte[result.length][];
for(int i = 0; i < result.length; i++){
    resultByte[i] = result[i].getBytes();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消