Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Split a string into characters

Thread view: 
zakhan@gmail.com - 18 Nov 2006 03:53 GMT
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"



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.