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!
Arne Vajhøj - 18 Nov 2006 03:59 GMT
> I'm trying to do an unbelievably simple thing but it's not working.
>
[quoted text clipped - 13 lines]
> 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
Mark Jeffcoat - 18 Nov 2006 04:02 GMT
> I'm trying to do an unbelievably simple thing but it's not working.
>
[quoted text clipped - 13 lines]
> 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.

Signature
Mark Jeffcoat
Austin, TX
Jeffrey Schwab - 18 Nov 2006 04:06 GMT
> I'm trying to do an unbelievably simple thing but it's not working.
>
[quoted text clipped - 13 lines]
> 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.
仁者无敌 - 18 Nov 2006 09:16 GMT
> 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.
On 11月18日, 下午12时06分, Jeffrey Schwab <j...@schwabcenter.com>
wrote:
> zak...@gmail.com wrote:
> > I'm trying to do an unbelievably simple thing but it's not working.
[quoted text clipped - 16 lines]
>
> This construct is called negative look-behind.- 隐藏被引用文字 -- 显示引用的文字 -
Jeffrey Schwab - 18 Nov 2006 04:07 GMT
> I'm trying to do an unbelievably simple thing but it's not working.
>
[quoted text clipped - 13 lines]
> 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.
trippy - 18 Nov 2006 19:54 GMT
> I'm trying to do an unbelievably simple thing but it's not working.
>
[quoted text clipped - 15 lines]
>
> Thanks!
char[] chArr = yourString.toCharArray();

Signature
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"