简体   繁体   English

使用正则表达式在java中拆分字符串

[英]splitting string in java using regex

How can I split following string in two strings? 如何在两个字符串中拆分后面的字符串?

input: 输入:

00:02:05,130 --> 00:02:10,130

output: 输出:

00:02:05,130
00:02:10,130

i tried this piece of code: 我尝试了这段代码:

String[] tokens = my.split(" --> ");
    System.out.println(tokens.length);
    for(String s : tokens)
        System.out.println(s);

but the out put is just the first part, what is wrong? 但是输出只是第一部分,出了什么问题?

You could use the String split() : 你可以使用String split()

String str = "00:02:05,130 --> 00:02:10,130";
String[] str_array = str.split(" --> ");
String stringa = str_array[0]; 
String stringb = str_array[1];

You may want to have a look at the following: Split Java String into Two String using delimiter 您可能需要查看以下内容: 使用分隔符将Java字符串拆分为两个字符串

尝试这个

String[] arr = str.split("\\s*-->\\s*");

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

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