I need to split a String into an array of single character Strings.
我需要将一个字符串分割成一个字符串数组。
Eg, splitting "cat" would give the array "c", "a", "t"
例如,分裂的“猫”会给数组“c”、“a”、“t”
10 个解决方案
#1
100
"cat".split("(?!^)")
This will produce
这将产生
array ["c", "a", "t"]
array[“c”、“a”、“t”)
#2
87
"cat".toCharArray()
But if you need strings
但如果你需要字符串。
"cat".split("")
Edit: which will return an empty first value.
编辑:它将返回一个空的第一个值。
#3
35
String str = "cat";
char[] cArray = str.toCharArray();
#5
2
If characters beyond Basic Multilingual Plane are expected on input (some CJK characters, new emoji...), approaches such as "a????b".split("(?!^)")
cannot be used, because they break such characters (results into array ["a", "?", "?", "b"]
) and something safer has to be used:
如果字符超出基本多语种飞机预计输入(some CJK characters,"a????b".split("(?!^)")等新emoji...),方法不能used,因为他们这样的人物(results分解成数组["a","?",“?”而更安全的东西必须被使用:
"a????b".codePoints()
.mapToObj(cp -> new String(Character.toChars(cp)))
.toArray(size -> new String[size]);
#6
1
An efficient way of turning a String into an array of one-character Strings would be to do this:
将字符串转换成一个字符串数组的有效方法是:
String[] res = new String[str.length()];
for (int i = 0; i < str.length(); i++) {
res[i] = Character.toString(str.charAt(i));
}
However, this does not take account of the fact that a char
in a String
could actually represent half of a Unicode code-point. (If the code-point is not in the BMP.) To deal with that you need to iterate through the code points ... which is more complicated.
然而,这并没有考虑到字符串中的字符实际上可以表示Unicode代码点的一半。(如果代码点不在BMP中)。要解决这个问题,您需要遍历代码点……这是更为复杂。
This approach will be faster than using String.split(/* clever regex*/)
, and it will probably be faster than using Java 8+ streams. It is probable faster than this:
这种方法将比使用字符串更快。split(/*巧妙的regex*/),可能比使用Java 8+流要快。它可能比这更快:
String[] res = new String[str.length()];
int 0 = 0;
for (char ch: str.toCharArray[]) {
res[i++] = Character.toString(ch);
}
because toCharArray
has to copy the characters to a new array.
因为toCharArray必须将字符复制到一个新的数组中。
#7
1
To sum up the other answers...
总结一下其他答案……
This works on all Java versions:
这适用于所有Java版本:
"cat".split("(?!^)")
This only works on Java 8 and up:
这只适用于Java 8和以上版本:
"cat".split("")
#8
0
If the original string contains supplementary Unicode characters, then split()
would not work, as it splits these characters into surrogate pairs. To correctly handle these special characters, a code like this works:
如果原始字符串包含补充的Unicode字符,那么split()将不起作用,因为它将这些字符分割为代理对。要正确处理这些特殊字符,可以使用如下代码:
String[] chars = new String[stringToSplit.codePointCount(0, stringToSplit.length())];
for (int i = 0, j = 0; i < stringToSplit.length(); j++) {
int cp = stringToSplit.codePointAt(i);
char c[] = Character.toChars(cp);
chars[j] = new String(c);
i += Character.charCount(cp);
}
#9
-1
Maybe you can use a for loop that goes through the String content and extract characters by characters using the charAt
method.
也许您可以使用一个for循环来遍历字符串内容并使用charAt方法通过字符提取字符。
Combined with an ArrayList<String>
for example you can get your array of individual characters.
例如,结合ArrayList
#10
-2
for(int i=0;i<str.length();i++)
{
System.out.println(str.charAt(i));
}
#1
100
"cat".split("(?!^)")
This will produce
这将产生
array ["c", "a", "t"]
array[“c”、“a”、“t”)
#2
87
"cat".toCharArray()
But if you need strings
但如果你需要字符串。
"cat".split("")
Edit: which will return an empty first value.
编辑:它将返回一个空的第一个值。
#3
35
String str = "cat";
char[] cArray = str.toCharArray();
#4
#5
2
If characters beyond Basic Multilingual Plane are expected on input (some CJK characters, new emoji...), approaches such as "a????b".split("(?!^)")
cannot be used, because they break such characters (results into array ["a", "?", "?", "b"]
) and something safer has to be used:
如果字符超出基本多语种飞机预计输入(some CJK characters,"a????b".split("(?!^)")等新emoji...),方法不能used,因为他们这样的人物(results分解成数组["a","?",“?”而更安全的东西必须被使用:
"a????b".codePoints()
.mapToObj(cp -> new String(Character.toChars(cp)))
.toArray(size -> new String[size]);
#6
1
An efficient way of turning a String into an array of one-character Strings would be to do this:
将字符串转换成一个字符串数组的有效方法是:
String[] res = new String[str.length()];
for (int i = 0; i < str.length(); i++) {
res[i] = Character.toString(str.charAt(i));
}
However, this does not take account of the fact that a char
in a String
could actually represent half of a Unicode code-point. (If the code-point is not in the BMP.) To deal with that you need to iterate through the code points ... which is more complicated.
然而,这并没有考虑到字符串中的字符实际上可以表示Unicode代码点的一半。(如果代码点不在BMP中)。要解决这个问题,您需要遍历代码点……这是更为复杂。
This approach will be faster than using String.split(/* clever regex*/)
, and it will probably be faster than using Java 8+ streams. It is probable faster than this:
这种方法将比使用字符串更快。split(/*巧妙的regex*/),可能比使用Java 8+流要快。它可能比这更快:
String[] res = new String[str.length()];
int 0 = 0;
for (char ch: str.toCharArray[]) {
res[i++] = Character.toString(ch);
}
because toCharArray
has to copy the characters to a new array.
因为toCharArray必须将字符复制到一个新的数组中。
#7
1
To sum up the other answers...
总结一下其他答案……
This works on all Java versions:
这适用于所有Java版本:
"cat".split("(?!^)")
This only works on Java 8 and up:
这只适用于Java 8和以上版本:
"cat".split("")
#8
0
If the original string contains supplementary Unicode characters, then split()
would not work, as it splits these characters into surrogate pairs. To correctly handle these special characters, a code like this works:
如果原始字符串包含补充的Unicode字符,那么split()将不起作用,因为它将这些字符分割为代理对。要正确处理这些特殊字符,可以使用如下代码:
String[] chars = new String[stringToSplit.codePointCount(0, stringToSplit.length())];
for (int i = 0, j = 0; i < stringToSplit.length(); j++) {
int cp = stringToSplit.codePointAt(i);
char c[] = Character.toChars(cp);
chars[j] = new String(c);
i += Character.charCount(cp);
}
#9
-1
Maybe you can use a for loop that goes through the String content and extract characters by characters using the charAt
method.
也许您可以使用一个for循环来遍历字符串内容并使用charAt方法通过字符提取字符。
Combined with an ArrayList<String>
for example you can get your array of individual characters.
例如,结合ArrayList
#10
-2
for(int i=0;i<str.length();i++)
{
System.out.println(str.charAt(i));
}