-5

We can convert char[] to a String with below code:

char[] c={'A','B','C'};
String s=new String(c);

Traditional Solution: Is looping through each char in char array and converting a char to String and then storing it in String each in String array.

But how to convert it from a character array to String array using predefined methods.

char[] to String[]

Q). Is there any prefined methods to convert?

Steven Henry
  • 101
  • 2
  • 13
  • 4
    You really have no idea? A for loop would do. Try something. – JB Nizet Jun 15 '18 at 10:14
  • May I know why my question is downvoted? – Steven Henry Jun 15 '18 at 10:14
  • 6
    Because it shows a lack of research. This is a very simple problem, and apparently you haven't tried anything to solve it. – JB Nizet Jun 15 '18 at 10:15
  • I didnt find any predefined methods in java neither in String class nor in Character class – Steven Henry Jun 15 '18 at 10:18
  • 1
    You can then safely assume that there isn't one. – JB Nizet Jun 15 '18 at 10:24
  • 4
    Iterate `c[]`. Put `String.valueOf(...)` into `s[]` – QBrute Jun 15 '18 at 10:29
  • @JB Nizet, no need to be THAT harsh on a beginner :) @StevenHenry Define a new String array of size of your character array's size. Then use `FOR` loop. Iterate through your character array. In every step of iteration you can set i-th element of String array using i-th element of character array. You can use `String.valueOf(char)`. Posting the actual code would not teach you anything. If you still struggle with it, please paste the code and we will be happy to assist. – Pijotrek Jun 15 '18 at 10:31
  • As a beginner, I'm not feeling motivated to post questions in StackOverFlow – Steven Henry Jun 15 '18 at 10:51
  • What is the difference between my question and the below one. https://stackoverflow.com/questions/8172420/how-to-convert-a-char-to-a-string – Steven Henry Jun 15 '18 at 10:53
  • 1
    @StevenHenry The difference is, that the question you linked existed before you posted yours. The reason why you are getting downvotes is that you have showed no research effort. Please have a look at [ask]. – Jaroslaw Pawlak Jun 15 '18 at 11:07
  • @JaroslawPawlak I believe that convertion of char to String is different fro char[] to String[] – Steven Henry Jun 15 '18 at 11:14
  • Possible duplicate of [How to convert a char to a String?](https://stackoverflow.com/questions/8172420/how-to-convert-a-char-to-a-string) – EJoshuaS - Stand with Ukraine Jun 15 '18 at 15:10
  • Why do you want a string array instead of just a standard string? What array should your example be converted to? – EJoshuaS - Stand with Ukraine Jun 15 '18 at 15:12
  • This is not a duplicate question. Can someone remove duplicate mark for this question? – Steven Henry Jun 15 '18 at 18:03
  • Here is the article to **[Convert Char Array To String In Java](https://www.tutorialcup.com/java/convert-char-array-to-string-in-java.htm)** this will help you answer your question – Rahul Gupta May 14 '21 at 17:26

2 Answers2

4

you can try this

create a new class in eclipse and paste main function code and try to run it.

   public class test {

    public static void main(String[] args) {
        char[] c={'A','B','C'};
        String[] stringarr = new String[c.length]; //initialised
        int i=0; 
        for (char d : c) {

            stringarr[i]=Character.toString(d);
            i++;
        }

        for (String string : stringarr) {
            System.out.println(string);
        }

    }



}

->initialised a string array with the storage length based on character array

->foreach loop for character array c and using Character.toString() to convert char to string

->finally printing the string array using a foreach loop .

Madhu Nair
  • 428
  • 1
  • 7
  • 20
  • @Pijotrek let him replicate and any way you guys have given him a very hard time. – Madhu Nair Jun 15 '18 at 10:35
  • did I? Where exactly :) I told him what to do without giving him a solution – Pijotrek Jun 15 '18 at 10:36
  • 2
    "you can try this" is not an answer,without explanation it values nothing – azro Jun 15 '18 at 10:36
  • @Pijotrek tell me what do you want me to do ? – Madhu Nair Jun 15 '18 at 10:37
  • 1
    @MadhuNair as azro pointed out, pasting a solution without saying a word is not a good idea. The OP will probably copy-paste it, having nothing learnt. This is not helping him in the long run. Not mentioning that he probably hasn't tried anything on his own which is already a bad, bad approach... – Pijotrek Jun 15 '18 at 10:40
  • @Pijotrek azro thank you for pointing it out .. i have added the explanation. – Madhu Nair Jun 15 '18 at 10:46
  • Thank you @MadhuNair.... I know how to do it this way...But my question is very clear that Does Java provided any predefined methods to convert char[] to String[] – Steven Henry Jun 15 '18 at 11:19
1
char[] charArray={'A','B','C'}; // Character array initialized
/**
   *Below line will first convert a charArray to string using 
   *String(char[]) constructor and using String class method  
   *split(regularExpression) the converted string will
   *then be splited with empty string literal delimiter which in turn 
   *returns String[] 
   **/
String[] result=new String(charArray).split("");
Steven Henry
  • 101
  • 2
  • 13
  • 2
    While this code snippet may be the solution, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Narendra Jadhav Jun 15 '18 at 14:17