I think this is an easy question, but I am not able to find a simple solution (say, less than 10 lines of code :)
我认为这是一个简单的问题,但我无法找到一个简单的解决方案(例如,少于10行代码:)
I have a String
such as "thisIsMyString"
and I need to convert it to a String[] {"this", "Is", "My", "String"}
.
我有一个字符串,比如“thisIsMyString”,我需要将它转换为一个字符串[]{"this", "Is", "My", "String"}。
Please notice the first letter is not uppercase.
请注意第一个字母不是大写。
6 个解决方案
#1
91
You may use a regexp with zero-width positive lookahead - it finds uppercase letters but doesn't include them into delimiter:
您可以使用一个带零宽度正朝前的regexp—它查找大写字母,但不将它们包含到分隔符中:
String s = "thisIsMyString";
String[] r = s.split("(?=\\p{Upper})");
Y(?=X)
matches Y
followed by X
, but doesn't include X
into match. So (?=\\p{Upper})
matches an empty sequence followed by a uppercase letter, and split
uses it as a delimiter.
Y(?=X)匹配Y后面跟着X,但不包含X。所以(?=\ p{Upper})匹配一个空序列,然后是大写字母,split将它用作分隔符。
See javadoc for more info on Java regexp syntax.
有关Java regexp语法的更多信息,请参见javadoc。
EDIT: By the way, it doesn't work with thisIsMyÜberString
too. For non-ASCII uppercase letters you need a Unicode uppercase character class instead of POSIX one:
编辑:顺便说一下,它也不能用thisIsMyUberString。对于非ascii大写字母,您需要一个Unicode大写字符类而不是POSIX字符类:
String[] r = s.split("(?=\\p{Lu})");
#2
16
String[] camelCaseWords = s.split("(?=[A-Z])");
#3
11
For anyone that wonders how the Pattern is when the String to split might start with an upper case character:
对于任何想知道分割字符串的模式的人来说,可以从大写字符开始:
String s = "ThisIsMyString";
String[] r = s.split("(?<=.)(?=\\p{Lu})");
System.out.println(Arrays.toString(r));
gives: [This, Is, My, String]
给出:[这个,是,我的,字符串]
#4
5
Since String::split
takes a regular expression you can use a look-ahead:
由于String::split接受一个正则表达式,您可以使用一个look forward:
String[] x = "thisIsMyString".split("(?=[A-Z])");
#5
0
Try this;
试试这个;
static Pattern p = Pattern.compile("(?=\\p{Lu})");
String[] s1 = p.split("thisIsMyFirstString");
String[] s2 = p.split("thisIsMySecondString");
...
#6
0
This regex will split on Caps, omitting the first. So it should work for camel-case and proper-case.
这个regex将在大写上分裂,省略第一个。因此,它应该适用于骆驼箱和手提箱。
(?<=.)(?=(\\p{Upper}))
TestText = Test, Text
thisIsATest = this, Is, A, Test
#1
91
You may use a regexp with zero-width positive lookahead - it finds uppercase letters but doesn't include them into delimiter:
您可以使用一个带零宽度正朝前的regexp—它查找大写字母,但不将它们包含到分隔符中:
String s = "thisIsMyString";
String[] r = s.split("(?=\\p{Upper})");
Y(?=X)
matches Y
followed by X
, but doesn't include X
into match. So (?=\\p{Upper})
matches an empty sequence followed by a uppercase letter, and split
uses it as a delimiter.
Y(?=X)匹配Y后面跟着X,但不包含X。所以(?=\ p{Upper})匹配一个空序列,然后是大写字母,split将它用作分隔符。
See javadoc for more info on Java regexp syntax.
有关Java regexp语法的更多信息,请参见javadoc。
EDIT: By the way, it doesn't work with thisIsMyÜberString
too. For non-ASCII uppercase letters you need a Unicode uppercase character class instead of POSIX one:
编辑:顺便说一下,它也不能用thisIsMyUberString。对于非ascii大写字母,您需要一个Unicode大写字符类而不是POSIX字符类:
String[] r = s.split("(?=\\p{Lu})");
#2
16
String[] camelCaseWords = s.split("(?=[A-Z])");
#3
11
For anyone that wonders how the Pattern is when the String to split might start with an upper case character:
对于任何想知道分割字符串的模式的人来说,可以从大写字符开始:
String s = "ThisIsMyString";
String[] r = s.split("(?<=.)(?=\\p{Lu})");
System.out.println(Arrays.toString(r));
gives: [This, Is, My, String]
给出:[这个,是,我的,字符串]
#4
5
Since String::split
takes a regular expression you can use a look-ahead:
由于String::split接受一个正则表达式,您可以使用一个look forward:
String[] x = "thisIsMyString".split("(?=[A-Z])");
#5
0
Try this;
试试这个;
static Pattern p = Pattern.compile("(?=\\p{Lu})");
String[] s1 = p.split("thisIsMyFirstString");
String[] s2 = p.split("thisIsMySecondString");
...
#6
0
This regex will split on Caps, omitting the first. So it should work for camel-case and proper-case.
这个regex将在大写上分裂,省略第一个。因此,它应该适用于骆驼箱和手提箱。
(?<=.)(?=(\\p{Upper}))
TestText = Test, Text
thisIsATest = this, Is, A, Test