Accessing a specific position in a string of delimited data
From: Aki Laukkanen (aki.laukkanenREMOVE_THIS_at_helsinki.fi)
Date: 05/26/04
- Next message: Aki Laukkanen: "Re: Possible Loss Of Precision??"
- Previous message: metfan: "Eclipse M9: Why I can't start the java debugger?"
- Next in thread: Pimousse: "Re: Accessing a specific position in a string of delimited data"
- Reply: Pimousse: "Re: Accessing a specific position in a string of delimited data"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Aki Laukkanen: "Re: Possible Loss Of Precision??"
- Previous message: metfan: "Eclipse M9: Why I can't start the java debugger?"
- Next in thread: Pimousse: "Re: Accessing a specific position in a string of delimited data"
- Reply: Pimousse: "Re: Accessing a specific position in a string of delimited data"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|