如何将字符数组转换为字符串数组?
例如,"Text not text" → "Text not text" 作为字符数组 → "Text" "not" "text"
我知道如何将 "Text not text" → "Text not text"
,但不知道如何将
"Text not text" 作为字符数组 → "Text" "not" "text"
以下是代码示例,但它不起作用。
public class main {
public static void main(String[] args) {
StringBuffer inString = new StringBuffer("text not text");
int n = inString.toString().replaceAll("[^a-zA-ZА-Я а-я]", "")
.split(" ").length;
char[] chList = inString.toString().toCharArray();
System.out.print("Text splited by chars - ");
for (int i = 0; i < chList.length; i++) {
System.out.print(chList[i] + " ");
}
System.out.println();
String[] temp = new String[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < chList.length; j++) {
if (chList[j] != ' ') {
temp[i] = new String(chList);
}
}
System.out.println(temp[i]);
}
}
}
String[n]
?你是想为每个字符创建一个字符串吗?还是想要一个由空格组成的字符串,长度为 n+1? - BobTheBuilder