标题翻译
Java split with the characters that are not the indicated delimiters for .csv file
问题
我有这个字符串:
String newstring = "2018 Panini PRIZM DEVONTE GRAHAM SILVER Prizm #288 ROOKIE PSA 10 GEM MT HORNETS /split/ 2017-18 Panini Prizm Prizms Silver #16 Jayson Tatum Rookie PSA 10 Gem Mint RC";
我目前正在使用 split 函数 `newstring.split("/split/")` 来获取两个单独的字符串,然后在 .csv 文件上输出,这个方法效果良好。
然而,现在我有了这个字符串:
String anotherstring = " 2017-18 Panini Prizm Prizms Silver #16 Jayson Tatum Rookie PSA 10 Gem Mint RC /split/ Ja Morant prizm rc , silver, base, college, PSA 10? Great cards no scratches";
如果我对 `anotherstring` 使用上面的代码,我会得到 6 个字符串,而不是我期望的 2 个。从我所测试的情况来看,split 函数也按逗号符进行了分割,这不是我指定的分隔符。我认为我的问题是我试图将其导出为 .csv 文件。但是我如何能够将文件导出为 .csv 格式,同时又不让值散布开?
**编辑:**
以下是当我运行该代码时在 .csv 文件中得到的字符串:
2017-18 Panini Prizm Prizms Silver #16 Jayson Tatum Rookie PSA 10 Gem Mint RC
Ja Morant prizm rc
silver
base
college
?? PSA 10?Great cards no scratches
英文翻译
I have this string:
String newstring = "2018 Panini PRIZM DEVONTE GRAHAM SILVER Prizm #288 ROOKIE PSA 10 GEM MT HORNETS /split/ 2017-18 Panini Prizm Prizms Silver #16 Jayson Tatum Rookie PSA 10 Gem Mint RC"
I am currently using the split function as newstring.split("/split/")
to obtain the two separate strings, then output as on a .csv file, and it works fine.
However, now I am given this string:
String anotherstring = " 2017-18 Panini Prizm Prizms Silver #16 Jayson Tatum Rookie PSA 10 Gem Mint RC /split/ Ja Morant prizm rc , silver, base, college, PSA 10? Great cards no scratches"
If I use the code above on anotherstring
, I will have 6 strings, but not 2 as I expect. From what I tested, the split function also split by the comma sign which was not what I indicated as the delimiter. I think that my problem is that I am trying to export it as a .csv file. But how can I still export the file as .csv but not getting the values sprinkled?
EDIT:
Here are the Strings that I obtained in the .csv file when I run the code:
2017-18 Panini Prizm Prizms Silver #16 Jayson Tatum Rookie PSA 10 Gem Mint RC
Ja Morant prizm rc
silver
base
college
?? PSA 10? Great cards no scratches
答案1
得分: 0
你可以在 CSV 文件内部包含逗号,只需确保整个数据片段被引用起来。以下是一个示例:
1,2,a,b,3 将会成为 5 个字符串,然而 1,2,"a,b",3 仅为 4 个字符串。
英文翻译
You can include commas inside csv's you just need to ensure that the entire piece of data is quoted. Here is an example:
1,2,a,b,3 will be 5 string however 1,2,"a,b",3 is only 4 strings.
专注分享java语言的经验与见解,让所有开发者获益!
评论