我正在try 使用分隔符拆分字符串,对元素执行操作,然后使用相同的分隔符再次连接在一起.
String[] split = input.split(deli); // split on the string
// Do stuff with split
return String.join(delimiter,split); // join with the same string
This works fine, unless the used deli is '\t' (tab) (and probably other similar ones).
Because String.join refers to the delimiter as a 'charsequence' might this be the cause?
例如:
var deli="\t";
var split = data.split(deli); // splits properly
var join = String.join(deli,split); // joins with sequence instead of char
var it = join.split(deli); // doesn't work
Is this normal? If so, how can I best work around it?
For now i'm doing this.
var deli="\t";
var split = data.split(deli); // splits properly
if( deli.equalsIgnoreCase("\\t"))
return String.join(String.valueOf((char)9),items);
return String.join(deli,items);
分隔符是从一个XML文件属性读取的,它被读取为"\\t",而不是"\t"(在ide调试中,它显示为\t).这是意料之中的,但因为分手总是奏效,所以从来没有想过这一点.
// First split works, second doesn't
var input ="[2023-04-25 07:31:26.175]\t1\tgeneric:qsr2150|par\t1770.112, 10.25, 9.043";
var deli = "\\t";
String[] split = input.split(deli); // splits properly
var join = String.join(deli,split);
var split2 = join.split(deli); // doesn't split
// Both splits work
var input ="[2023-04-25 07:31:26.175]\t1\tgeneric:qsr2150|par\t1770.112, 10.25, 9.043";
var deli = "\t";
String[] split = input.split(deli);
var join = String.join(deli,split);
var split2 = join.split(deli); // splits properly
所以我的问题的解决方案是将属性正确地转换为‘制表符’.
但是,为什么第一个分拆可行,而第二个分拆行不通?你本以为第一次分手就会失败.