tags:

views:

71

answers:

7

i have a string="name"; i want to convert in an string array.\ how to do it? is there any java built in function? manually i can do it but i'm searching for a java built in function

i want an array where each character of the string will be a string. like char 'n' will be now string "n" stored in an array

A: 

Convert it to type Char?

http://www.javadb.com/convert-string-to-character-array

Aidanc
Why is this downvoted ? It kind of looks like a solution to what had been asked (for as much as I can understand it anyway)...Even though I think you meant `char[]` =)
Axel
A: 

String array = array of characters ?

Or do you have a string with multiple words each of which should be an array element ?

String[] array = yourString.split(wordSeparator);

thelost
i want an array where each character of the string will be a string. like char 'n' will be now string "n" stored in an array
riyana
A: 
String strName = "name";
String[] strArray = new String[] {strName};
System.out.println(strArray[0]); //prints "name"

The second line allocates a String array with the length of 1. Note that you don't need to specify a length yourself, such as:

String[] strArray = new String[1];

instead, the length is determined by the number of elements in the initalizer. Using

String[] strArray = new String[] {strName, "name1", "name2"};

creates an array with a length of 3.

f1sh
Does not really answer the question.
atamanroman
That was not clear when I answered the question. Was upvoted before.
f1sh
A: 

I guess there is simply no need for it, as it won't get more simple than

String[] array = {"name"};

Of course if you insist, you could write:

static String[] convert(String... array) {
   return array;
}

String[] array = convert("name","age","hobby"); 

[Edit] If you want single-letter Strings, you can use:

String[] s = "name".split("");

Unfortunately s[0] will be empty, but after this the letters n,a,m,e will follow. If this is a problem, you can use e.g. System.arrayCopy in order to get rid of the first array entry.

Landei
i want an array where each character of the string will be now itself a string. like char 'n' will be now string "n" stored in an array
riyana
See my [Edit]...
Landei
+5  A: 

To start you off on your assingment, String.split splits strings on a regular expression, this expression may be the empty string:

String[] ary = "abc".split("");

Yields the array:

(java.lang.String[]) [, a, b, c]

Getting rid of the empty 1st entry is left as an exercise for the reader :-)

rsp
A: 

Assuming you really want an array of single-character strings (not a char[] or Character[])

1. Using a regex:

public static String[] singleChars(String s) {
    return s.split("(?!^)");
}

The zero width negative lookahead prevents the pattern matching at the start of the input, so you don't get a leading empty string.

2. Using Guava:

import java.util.List;

import org.apache.commons.lang.ArrayUtils;

import com.google.common.base.Functions;
import com.google.common.collect.Lists;
import com.google.common.primitives.Chars;

// ...

public static String[] singleChars(String s) {
    return
        Lists.transform(Chars.asList(s.toCharArray()),
                        Functions.toStringFunction())
             .toArray(ArrayUtils.EMPTY_STRING_ARRAY);
}
finnw
A: 
String data = "abc";
String[] arr = explode(data);

public void String[] explode(String s) {
    String[] arr = new String[data.size()];
    for(int i = 0; i < String.size(); i++)
    {
        arr[i] = String.valueOf(data.charAt(i));
    }
    return arr;
}
atamanroman
I was not sure if String.split("") would split each char or nothing. Split by "" is much easier then. (I HAVE to install java here!)
atamanroman