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

标签 java arrays string char

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

例如 "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/

相关文章:

java - 将多个字符串组装成一个

java - 没有用于 Tomcat 的实体管理器的持久性提供程序

c - C : are they jagged? 中的多维数组

java - Java 中的 URL 路由

javascript - 将查询结果传递到另一个页面的最佳方式

java - Android中如何获取ArrayList的模型数据类型?

javascript - 格式化一些数据以作为多连字符字符串名称传递

string - 为什么 Kotlin 中 null + null 的类型是隐式 String?

java - 调用 Restful Webservice 时 Uri 不是绝对异常

Java对子类的静态反射