Accessing a specific position in a string of delimited data

From: Aki Laukkanen (aki.laukkanenREMOVE_THIS_at_helsinki.fi)
Date: 05/26/04


Date: Wed, 26 May 2004 10:50:36 +0300

I've been puzzled by the following problem for quite some time now:
Suppose you have an ASCII file of thousands of lines of delimited data,
like this:

12153753 000775 027 0005 0013 0356 0894 0010 1499 0217 0000 001 001 ...
data ^ ^
         delimiter (space) position x

What you want is a summary of certain data at a specific position on
each line.
I've used the FileReader and BufferedReader classes to read each line
into a StringBuffer, but have run into problems trying to extract
position x, where x equals the number of delimiters before the data I
want to access.
Using the indexOf() -function seems a bit complicated and I've failed to
achieve the desired results with this. I tried using the subString
function to get a string from index of(delimiter x) to index of
(delimiter x+1), but for some reason it does not work right. I also
tried using nested indexOf -functions, like:

indexOf(delimiter, indexOf(delimiter)) etc.

That does not seem to work too well either plus it gets awfully
complicated to read.

My next solution to this was to use a for-loop like this:

for (int i = 0; i < positionX ;i++){
//delete up to the first delimiter
dataStringBuffer.delete(0, dataStringBuffer.indexOf(delimiter);
}
//delete the end of the string after the desired data
dataStringBuffer.delete(dataStringBuffer.indexOf(delimiter),
dataStringBuffer.length());

But that does not work correctly either.

Does anyone have a useful solution to this?

-- 
-Aki "Sus" Laukkanen


Relevant Pages

  • Re: Programmers unpaid overtime.
    ... tokenizing a string correctly. ... ability to specify a set of delimiter tokens, ... >> postmodern attack on language itself, ... An the scientists, in turn, think a lot of that artistic talk about ...
    (comp.programming)
  • Re: Surprise in StrConv using vbProperCase
    ... > Your code will capitalize the first letter of a string. ... Use Split(string into array of words using blank as delimiter); ...
    (microsoft.public.access.modulesdaovba)
  • Re: VB vs C#
    ... Dim delimiter As String ... Dim testname As String ...
    (microsoft.public.dotnet.general)
  • Re: Clarification
    ... With your strings in column A, select the data and then pick Text to Columns delimited with space and comma as the delimiter. ... And a bit of advice from one who's often been there and done that: forget about the concatenation - leave the data separated by columns. ... With your string in column A, Text to Columns fixed width could give you ... >> re-assemble them using concatenation and copying down ...
    (microsoft.public.excel.misc)
  • Re: INSERT Query problem with Quotes & Apostrophes
    ... Position of match string, ... Dim intI As Integer, intLenC As Integer, intLenM As Integer ... An array of strings and an optional delimiter ... Dim strWork As String ...
    (microsoft.public.access.modulesdaovba)