Split byte array with string?
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();
}
精彩评论