[英]String.split more regex chars
我需要将我的字符串分成两部分,例如:
String myString = "the_string";
String splitted[] = myString.split("_");
多数民众赞成在确定,但如果我的字符串包含此: myString = "the____string";
它不起作用,我不知道如何确保这一点,谢谢;
String.split
的定界符参数是一个正则表达式。 如果要分割多个下划线之一,请使用myString.split("_+")
。
如果无论分隔符是否重复,都总是需要两个元素,则myString.split("_+", 2)
String a = "hello_there"
String b = "hello___there"
String c = "hello____there___how__are_you"
a.split("_+"); // -> ["hello", "there"]
b.split("_+"); // -> ["hello", "there"]
c.split("_+"); // -> ["hello", "there", "how", "are", "you"]
a.split("_+", 2); // -> ["hello", "there"]
b.split("_+", 2); // -> ["hello", "there"]
c.split("_+", 2); // -> ["hello", "there___how__are_you"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.