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 / April 2007

Tip: Looking for answers? Try searching our database.

simple string parsing question

Thread view: 
Marc E - 08 Apr 2007 00:32 GMT
All,
 Been a looong time since i've done any string parsing in java. I seem to
remember there being a braindead simple way of turning a string like this:

100,1,1;2598,2,3;25467,1,3

into an array of arrays delimited first on the semicolon and then on the
comma.

Currently, i'm just stringTokenizing it on the semicolon and then doing
next() calls.

So...
StrTokenizer = new StrTokenizer("100,1,1;2598,2,3;25467,1,3",";");
List list = tokenizer.asList()

iterate over list
  StrTokenizer newTokenizer = new StrTokenizer(list.next(),",");
  List newlist = newTokenizer.asList();
   thisVal  = newlist.next();
   someOtherVal = newlist.next();

and so forth.

it's just damn ugly, so i'm wondering what the best/cleanest way to do this
in java is.

thanks all.

Marc
Marc E - 08 Apr 2007 00:37 GMT
ugggg, forget it. funny how you go looking in all your little commons helper
packages when it's right there in String.

> All,
>  Been a looong time since i've done any string parsing in java. I seem to
[quoted text clipped - 26 lines]
>
> Marc
CodeForTea@gmail.com - 08 Apr 2007 02:45 GMT
> ugggg, forget it. funny how you go looking in all your little commons helper
> packages when it's right there in String.
[quoted text clipped - 31 lines]
>
> - Show quoted text -

public static String[][] getArrays(String stringVal){
       String[] strings= stringVal.split(";");
       String[][]arrayOFarrays = new String[strings.length][];
       for (int i = 0; i < strings.length; i++) {
           arrayOFarrays[i] = strings[i].split(",");
       }
       return arrayOFarrays;
   }

I am not sure if it is any prettier, but another way of doing it.

Dinesh


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



©2008 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.