Results 1 to 10 of 10

Thread: Parsing Delimited Strings

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Resolved Parsing Delimited Strings

    I use the follwing method to parse delimited strings so I can get certain pieces:
    Code:
     public static String instanceSearch(String strIn, int intInstance, String strDelimiter)
     {
      String strToReturn = "";
      StringTokenizer t = new StringTokenizer(strIn,strDelimiter);
      int intCounter = 0;
      while(t.hasMoreTokens())
      { 
       intCounter++;
       if(intCounter == intInstance)
       {
        strToReturn =  t.nextToken().toString();
        continue;
       }
       t.nextToken();
      }
      return strToReturn; 
     }//End instanceSearch
    I call it using:
    strBAN = instanceSearch(strLineIn,4,",");
    This means I want the fourth item in the comma-delimeted string I call strLineIn.
    For this string:
    123,Bedon,Joe,21 ROCK PILE,NUEVO,CA,925679086,111,100.00,07/07/2005,1676268503
    I would return 21 ROCK PILE

    For this string:
    456,,,62 RIVERSIDE PLAZA,Podunk,NM,871204306,111,81.00,06/30/2005,1675376520
    I return NM

    I can not figure out why it is skipping the 2 empty items after the 456.

    Anyone have an idea?
    Last edited by rockies1; Jul 26th, 2005 at 08:46 AM. Reason: Closing
    Morgan
    teamtj@gmail.com - Home
    morgan.erickson@sprint.com - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Parsing Delimited Strings

    Seems like a lot just to get a piece of a String. Why not just do?
    Code:
    public String getSub(String s, int x, int y){
     return s.substring(x,y); 
    }

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Parsing Delimited Strings

    Try calling your method with

    "456,,,62 RIVERSIDE PLAZA,Podunk,NM,871204306,111,81.00,06/30/2005,1675376520"

    then this

    "456, , ,62 RIVERSIDE PLAZA,Podunk,NM,871204306,111,81.00,06/30/2005,1675376520"

  4. #4

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Re: Parsing Delimited Strings

    Because the length of the string changes every time and substring requires a specific starting point.

    I need the Nth item in a delimited list.
    Morgan
    teamtj@gmail.com - Home
    morgan.erickson@sprint.com - Work
    Using VB6 SP6 but trying to learn VB2005EE

  5. #5

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Re: Parsing Delimited Strings

    I can't change the values in the string as I am getting them from a vendor-provided file and loading them to oracle.

    I ended up re-writing the method to this:
    Code:
    	public static String instanceSearch(String strIn, int intInstance, String strDelimiter)
    	{
    		String strToReturn = "";
    		String strTempHolder = "";
    		String strTempIn = strIn + strDelimiter;
    		int intCounter = 0;
    		while(intCounter < intInstance)
    		{	
    			intCounter++;
    			strTempHolder = strTempIn.substring(0,strTempIn.indexOf(strDelimiter));
    			if(intCounter == intInstance)
    			{
    				strToReturn =  strTempHolder; 
    				break;
    			}
    			strTempIn = strTempIn.substring(strTempIn.indexOf(strDelimiter)+1);
    		}
    		return strToReturn;	
    	}//End instanceSearch
    It works, but is way less efficient, I believe.
    Morgan
    teamtj@gmail.com - Home
    morgan.erickson@sprint.com - Work
    Using VB6 SP6 but trying to learn VB2005EE

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Parsing Delimited Strings

    Posted by rockies1

    Because the length of the string changes every time and substring requires a specific starting point.

    I need the Nth item in a delimited list.
    Understandable.

  7. #7

  8. #8
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Parsing Delimited Strings

    How about this?
    Code:
    public static String getSub(String str, int i, String del){
     Scanner s = new Scanner(str).useDelimiter(del);
     int index = 0; 
     while(index < i){
     s.next();
     ++index;
     }
     return s.next(); 
    }

  9. #9

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Re: Parsing Delimited Strings

    Looks good but, unfortunately, I am stuck using java 1.4.2 and Scanner appeared in 1.5.

    Thank you for your help on this, I appreciate it!
    Morgan
    teamtj@gmail.com - Home
    morgan.erickson@sprint.com - Work
    Using VB6 SP6 but trying to learn VB2005EE

  10. #10
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Parsing Delimited Strings

    Too bad not up to date. I used 1.3 for a long time, didn't see a need to jump to 1.4.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width