0

Как я могу преобразовать массив символов в массив строк?

К примеру

"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]);
        }
    }

}

2 ответа 2

5
String[] arr = "Text not text".split(" ");

String[] strArray = new String[] { "Text not text" };
0

Я так понял, что нужно это:

char[] chars = {'T','e','x','t',' ','n','o','t',' ','t','e','x','t'};
String srt = new String(chars);
String[] split = srt.split(" ");

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.