按多个分隔符在Java中分割字符串

huangapple go评论66阅读模式
英文:

Split a String by multiple delimiters in java

问题

以下是翻译好的内容:

什么是通过多个分隔符拆分字符串的可能方法?StringTokenizer能帮助我实现这一点吗?

String str="list1|10456103|10456102|10456121#list2|10456105|10456122";
String str="list1|10513846#list2|";
String str3="list1#list2|10509855";
String str4="list2|10481812|";
String str5="list1|10396496|";
String str6="list1#list2|";

现在我应该能够仅提取长整型值

对于Str1Finallist=[10456103,10456102,10456121,10456105,10456122]
对于Str2Finallist=[10513846]
对于Str3Finallist=[10509855]
对于Str4Finallist=[10481812]
对于Str5Finallist=[10396496]
对于Str6Finallist=[]
英文:

what is the possible way to split a String by multiple delimiters? Is StringTokenizer can help me out to achieve this?

String str="list1|10456103|10456102|10456121#list2|10456105|10456122";
String str="list1|10513846#list2|";
String str3="list1#list2|10509855";
String str4="list2|10481812|";
String str5="list1|10396496|";
String str6="list1#list2|";

So now I should be able to extract only the long values :

For Str1  Finallist=[10456103,10456102,10456121,10456105,10456122]
For Str2  Finallist=[10513846]     
For Str3  Finallist=[10509855]
For Str4  Finallist=[10481812]
For Str5  Finallist=[10396496]
For Str6  Finallist[] 

答案1

得分: 1

你可以使用 Java 中的字符串 split 方法来将它们分割,然后检查是否为数字。

该字符串通过 #|, 进行分割,可以出现一次或多次。

然后对分割后的字符串进行数字检测,如下所示:

public static void main(String []args){
    String str = "list1|10456103|10456102|10456121#list2|10456105|10456122";
    
    String arr[] = str.split("[|,#]+");
    
    for (String s : arr) {
        try {
            int num = Integer.parseInt(s);
            System.out.println(num + " 是一个数字"); // 添加到列表
        } catch (Exception err) {
            System.out.println(s + " 不是一个数字"); // 不添加到列表
        }
    }
}
英文:

you can split them using the split method for String in java, then check if it's numeric or not.

the string is split by having # or | or , once or more than once.

then the split strings are tested to be numeric or not, as so:

    public static void main(String []args){
    String str="list1|10456103|10456102|10456121#list2|10456105|10456122";
    
    String arr[] = str.split("[|,#]+");
    
    for(String s: arr){
        try{
            int num=Integer.parseInt(s);
            System.out.println(num + " is a number"); //add to list
        }catch(Exception err) {
            System.out.println(s + " is not a number"); //don't add to list
        }
    }
 }

答案2

得分: 1

以下是翻译好的部分:

为了您的考虑,您还可以将它们即时转换为列表的映射以供以后处理。

它的工作方式如下:

  • 将每个字符串以 [|#] 分割
  • 仅筛选匹配 \\d+ 的数字
  • 转换为长整型
  • 并保存到列表中

LinkedHashMap 只是保留了最终映射中的处理顺序。实际上,这并不是必需的。

String[] strings = { str1, str2, str3, str4, str5, str6 };
Map<String, List<Long>> map = IntStream
        .range(0, strings.length)
        .boxed()
        .collect(Collectors.toMap(i -> "str" + (i + 1),
                i -> Arrays.stream(strings[i].split("[|#]"))
                        .filter(str -> str.matches("\\d+"))
                        .map(Long::valueOf)
                        .collect(Collectors.toList()),
                (a, b) -> a, LinkedHashMap::new));

map.entrySet().forEach(System.out::println);

输出结果如下:

str1=[10456103, 10456102, 10456121, 10456105, 10456122]
str2=[10513846]
str3=[10509855]
str4=[10481812]
str5=[10396496]
str6=[]
英文:

For your consideration you can also convert them to a map of lists on the fly for later processing.

It works as follows:

  • Split each String on [|#]
  • filter on numbers only matching on \\d+
  • convert to a long
  • and save to a list

The LinkedHashMap simply preserves processing order in the final map. It is not really required for this to work.

String[] strings = { str1, str2,str3,str4,str5,str6 };
Map&lt;String, List&lt;Long&gt;&gt; map = IntStream
		.range(0, strings.length)
		.boxed()
		.collect(Collectors.toMap(i-&gt;&quot;str&quot;+(i+1),
				i-&gt;Arrays.stream(strings[i].split(&quot;[|#]&quot;))
						.filter(str-&gt;str.matches(&quot;\\d+&quot;))
						.map(Long::valueOf)
						.collect(Collectors.toList()),
						(a,b)-&gt;a, LinkedHashMap::new));

map.entrySet().forEach(System.out::println);

Prints

str1=[10456103, 10456102, 10456121, 10456105, 10456122]
str2=[10513846]
str3=[10509855]
str4=[10481812]
str5=[10396496]
str6=[]

</details>



huangapple
  • 本文由 发表于 2020年7月27日 19:17:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63114169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定