Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

}
share|improve this question
Why do you create a String[n]? Do you want a String for each char? or String as number of spaces +1? – whoAmI Apr 22 at 11:50
thats possible when you will have to detect the words...! – anshulkatta Apr 22 at 11:50
n - number of words. – Anton Artemov Apr 22 at 11:53

4 Answers

up vote 0 down vote accepted

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);
}
share|improve this answer
true but possible when searched in dictionary , like word detector!! – anshulkatta Apr 22 at 11:52
But if space only beatwean words? – Anton Artemov Apr 22 at 12:20

Use the String.split() method.

share|improve this answer
This is certainly the simple answer if the question is "How do I split a sentence into words?" – Duncan Jones Apr 22 at 12:03
Quetion is how to conver char array back to words! – Anton Artemov Apr 22 at 12:08
Convert the char array to a String, and split the string. – JB Nizet Apr 22 at 12:43

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.

share|improve this answer
And if spaces onle beatwean words? – Anton Artemov Apr 22 at 12:23

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.

share|improve this answer
Not correct. Then my text will be done from same splited text – Anton Artemov Apr 22 at 12:30
@AntonArtemov Well... it is correct. Perhaps you've not explained all the rules of your assignment? – Duncan Jones Apr 22 at 12:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.