gpt4 book ai didi

java - 用字符串拆分字节数组?

转载 作者:行者123 更新时间:2023-11-30 06:35:04 26 4
gpt4 key购买 nike

我想实现这样的目标:

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

是否可以使用字节数组格式的字符串进行拆分?

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

最佳答案

当然,您可以将字节数组转换为字符串:

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();
}

关于java - 用字符串拆分字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6406542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com