Split a string into characters

Z

zakhan

I'm trying to do an unbelievably simple thing but it's not working.

I want to split a string into its constituent characters. I want to
split the string 83AV into 8,3,A,V. I use the following code:

<code>
String str = "83AV";
String[] strToks = str.split("");

for (String tok : strToks)
System.out.println(tok);
</code>

This gives me an extra "null" String in addition to the actual tokens.

Any idea how we can do this in a simple way? What regular expression
should I use for the split function?

Thanks!
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

I'm trying to do an unbelievably simple thing but it's not working.

I want to split a string into its constituent characters. I want to
split the string 83AV into 8,3,A,V. I use the following code:

<code>
String str = "83AV";
String[] strToks = str.split("");

for (String tok : strToks)
System.out.println(tok);
</code>

This gives me an extra "null" String in addition to the actual tokens.

Any idea how we can do this in a simple way? What regular expression
should I use for the split function?

str.toCharArray() will return a char[] and you can then
either use each char directly or use Character.toString on it.

Arne
 
M

Mark Jeffcoat

I'm trying to do an unbelievably simple thing but it's not working.

I want to split a string into its constituent characters. I want to
split the string 83AV into 8,3,A,V. I use the following code:

<code>
String str = "83AV";
String[] strToks = str.split("");

for (String tok : strToks)
System.out.println(tok);
</code>

This gives me an extra "null" String in addition to the actual tokens.

Any idea how we can do this in a simple way? What regular expression
should I use for the split function?

You're trying a bit too hard.

If you just need, as you say, the constituent characters,
just use String.toCharArray();

That returns char[]; if you want String[] instead, you
can step through the array applying the String(char[], int offset,
int value) constructor.
 
J

Jeffrey Schwab

I'm trying to do an unbelievably simple thing but it's not working.

I want to split a string into its constituent characters. I want to
split the string 83AV into 8,3,A,V. I use the following code:

<code>
String str = "83AV";
String[] strToks = str.split("");

for (String tok : strToks)
System.out.println(tok);
</code>

This gives me an extra "null" String in addition to the actual tokens.

Any idea how we can do this in a simple way? What regular expression
should I use for the split function?

str.split("(?<=\\w)");

This construct is called negative look-behind.
 
J

Jeffrey Schwab

I'm trying to do an unbelievably simple thing but it's not working.

I want to split a string into its constituent characters. I want to
split the string 83AV into 8,3,A,V. I use the following code:

<code>
String str = "83AV";
String[] strToks = str.split("");

for (String tok : strToks)
System.out.println(tok);
</code>

This gives me an extra "null" String in addition to the actual tokens.

Any idea how we can do this in a simple way? What regular expression
should I use for the split function?

str.split("(?<=\\w)");

This construct is called negative look-behind.
 
?

=?gb2312?B?yMrV387etdA=?=

str.split("(?<=\\w)");
This construct is called negative look-behind.
~~~~~~maybe positive look-behind


ref from java 5 api doc :
"
Special constructs (non-capturing)
(?:X) X, as a non-capturing group
(?idmsux-idmsux) Nothing, but turns match flags on - off
(?idmsux-idmsux:X) X, as a non-capturing group with the given flags
on - off
(?=X) X, via zero-width positive lookahead
(?!X) X, via zero-width negative lookahead
(?<=X) X, via zero-width positive lookbehind
(?<!X) X, via zero-width negative lookbehind
(?>X) X, as an independent, non-capturing group
"

Although I don't know what each item means.





I'm trying to do an unbelievably simple thing but it's not working.
I want to split a string into its constituent characters. I want to
split the string 83AV into 8,3,A,V. I use the following code:
<code>
String str = "83AV";
String[] strToks = str.split("");
for (String tok : strToks)
System.out.println(tok);
</code>
This gives me an extra "null" String in addition to the actual tokens.
Any idea how we can do this in a simple way? What regular expression
should I use for the split function? str.split("(?<=\\w)");

This construct is called negative look-behind.- Òþ²Ø±»ÒýÓÃÎÄ×Ö -- ÏÔʾÒýÓõÄÎÄ×Ö -
 
T

trippy

I'm trying to do an unbelievably simple thing but it's not working.

I want to split a string into its constituent characters. I want to
split the string 83AV into 8,3,A,V. I use the following code:

<code>
String str = "83AV";
String[] strToks = str.split("");

for (String tok : strToks)
System.out.println(tok);
</code>

This gives me an extra "null" String in addition to the actual tokens.

Any idea how we can do this in a simple way? What regular expression
should I use for the split function?

Thanks!

char[] chArr = yourString.toCharArray();

--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "All I Really Want" -- Alanis Morissette

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top