3

How can I can convert char array to string array?

For example "Text not text" → "Text not text" as char array → "Text" "not" "text"

I understand how to "Text not text" → "Text not text", but dont know how to

"Text not text" as char array → "Text" "not" "text"

Here is code example, but it does not work

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
  • Why do you create a String[n]? Do you want a String for each char? or String as number of spaces +1? Commented Apr 22, 2013 at 11:50
  • thats possible when you will have to detect the words...! Commented Apr 22, 2013 at 11:50

4 Answers 4

2

So you have a char array as following am I right:

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

Then what you want to get is separate words Text, not and text ??

if so, then do the following:

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

now strArray is your array.

Sign up to request clarification or add additional context in comments.

2 Comments

Not correct. Then my text will be done from same splited text
@AntonArtemov Well... it is correct. Perhaps you've not explained all the rules of your assignment?
1

Use the String.split() method.

3 Comments

This is certainly the simple answer if the question is "How do I split a sentence into words?"
Quetion is how to conver char array back to words!
Convert the char array to a String, and split the string.
0

The short sweet answer is from anvarik. However, if you need to show some working (maybe this is homework?), the following code will construct the list manually:

char[] chars = "text not text".toCharArray();

List<String> results = new ArrayList<String>();
StringBuilder builder = new StringBuilder();

for (int i = 0; i < chars.length; i++) {
  char c = chars[i];

  builder.append(c);

  if (c == ' ' || i == chars.length - 1) {
    results.add(builder.toString().trim());
    builder = new StringBuilder();
  }
}

for (String s : results) {
  System.out.println(s);
}

2 Comments

true but possible when searched in dictionary , like word detector!!
But if space only beatwean words?
-1

I think if you replace all the white spaces when you are converting "Text not text" with a $ symbol so the resulting string becomes 'T e x t$ n o t$ t e x t'

String ex= ex.replaceAll("\s","$");

and while converting it back you can replace the $ with white spaces again.

Other than this I can't seem to think any other way of how you can maintain the meanings of the words while splitting.

1 Comment

And if spaces onle beatwean words?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.