简体   繁体   English

Java处理字符串中的逗号

[英]Java dealing with commas in strings

I have fixed strings like: 我有固定的字符串,如:

KEY(1,One,Two,Three,table1)
KEY(1,Cat,Dog,Bat,Rabbit,table2)
KEY(1,Go,Do,table3)

I know that the fixed parts are: 我知道固定部分是:

  • the first four chars (until the ,) 前四个字符(直到)
  • the last three 最后三个

I am interested to get only the words inside these two parts without commas and store that in a LinkedList. 我感兴趣的是只获取这两部分中的单词而不用逗号并将其存储在LinkedList中。 For example: Cat Dog Bat Rabbit, or One Two Three. 例如:猫狗蝙蝠兔,或一二三。

Is there an easy way to get rid of the commas and store all values in a LinkedList? 有没有一种简单的方法来摆脱逗号并将所有值存储在LinkedList中?

List<String> list = new LinkedList<>();
for (String key : keys){
  String[] array = key.split(",");
  for (int i=1; i<array.length-1; i++){
    list.add(array[i].trim());
  }
}

or 要么

List<String> tmpList = Arrays.asList(str.split(","))
List<String> list = new LinkedList<>(tmpList.sublist(1, tmpList.size()-1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM