gpt4 book ai didi

java - 字符串到字符数组和字符串数组

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:29:35 24 4
gpt4 key购买 nike

如何将字符数组转换为字符串数组?

例如 "Text not text"→ "Text not text"as char array → "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]);
}
}

}

最佳答案

所以你有一个 char 数组,如下所示,我是对的:

char[] chars = new char[] {'T', 'e', 'x', 't', ' ', 'n', 'o', 't', ' ', 't', 'e', 'x', 't'};

然后你想要得到的是单独的单词Text, not and text ??

如果是,则执行以下操作:

String newString = new String(chars);
String[] strArray = newString.split(" ");

现在 strArray 是您的数组。

关于java - 字符串到字符数组和字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16146404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com