temp = line.split(",");
if (i < paymentFieldsMapIndex.size()) {
paymentFields.put(paymentFieldsMap.get(next).toString(), temp[i]);
}
此代码将逗号分隔的字符串拆分为子字符串,并使用生成的子字符串填充 HashMap 值。
一些子字符串值在生成的 HashMap 中看起来很有趣,看起来是由于 token 中存在逗号。
例如
,"英国伦敦",
在字符串中出现在HashMap中的样子
Key = key, Value = "伦敦
我认为 String split() 不会拆分包含定界符的子字符串(如果它们用双引号引起来)?
我也试过像这样转义嵌入的逗号
,"伦敦\,英国",
但是HashMap中的字符串看起来像
键 = 键,值 = "伦敦\
我是不是遗漏了什么,或者有什么办法可以解决这个问题?谢谢。
最佳答案
"I thought that String split(), will not break up substrings containing the delimiter if they are enclosed in double quotes?"
你这里的假设是不正确的。 split()
除了你传递给它的正则表达式之外什么都不关心。
如果您需要在尊重引号的同时拆分它,there are many ways to accomplish that .
关于java - 字符串拆分()无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11583699/