Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Splitting strings on whitespace in Delphi?



Permlink Replies: 4 - Last Post: Dec 23, 2016 11:59 AM Last Post By: Remy Lebeau (Te...
Bo Berglund

Posts: 757
Registered: 10/23/02
Splitting strings on whitespace in Delphi?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 23, 2016 12:05 AM
Is there a quick way to split a string of whitespace separated values
into the separate members?
I have to create a function to process a number of big data files where
numbers are stored in lines of 4-6 values using whitespace inbetween.
First I got a sample looking like this:
0.4167    0.3636    -14.1483    227.2260

Here the separators were 4 spaces so on each line I used (slDecode is a
TStringList):
sLine := StringReplace(sLine, '    ', #13, [rfReplaceAll]);
slDecode.Text := sLine;

Worked fine if a bit slow...
But then it failed on a file containing lines like this:
   0.000    0.000    7.000    0.000  29.6628

Here there are 3 leading spaces plus one separator is only 2 spaces
wide. So I had to modify the code:
sLine := Trim(sLine);
sLine := StringReplace(sLine, '    ', #13, [rfReplaceAll]);
sLine := StringReplace(sLine, '  ', #13, [rfReplaceAll]);
slDecode.Text := sLine;

This works in this case but now I realize I need something better,
which can deal with varying number of whitespace chars inbetween
numbers.
The test files are very big, like half a million lines and up, so I
cannot introduce a lot of code in the loop since processing time will
increase.

Is there any good and quick way to extract real data from a space
separated list without knowing beforehand the size of the whitespace
separators?
--

Bo Berglund
Sweden & Texas
now using XanaNews 1.20-0cfde51
GPG public key: http://tinyurl.com/jbmuutu
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Splitting strings on whitespace in Delphi?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 23, 2016 1:30 AM   in response to: Bo Berglund in response to: Bo Berglund
Bo Berglund wrote:

Is there a quick way to split a string of whitespace separated values
into the separate members?

You could use a TStringList with the proper separators and delimiters
set. Or you use StrUtils.SplitString.

--
Rudy Velthuis http://www.rvelthuis.de

"A conservative is a man who is too cowardly to fight and too
fat to run."
-- Elbert Hubbard
Bo Berglund

Posts: 757
Registered: 10/23/02
Re: Splitting strings on whitespace in Delphi?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 23, 2016 4:29 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy Velthuis (TeamB) wrote:

Bo Berglund wrote:

Is there a quick way to split a string of whitespace separated
values into the separate members?

You could use a TStringList with the proper separators and delimiters
set. Or you use StrUtils.SplitString.

I also asked in the FreePascal mail list and was suggested to use the
DelimitedText property when I add the line to the stringlist.
It actually worked right off for my test files without first trying to
set the delimiter (which would not work since the delimiter is varying
in length). In fact I tested replacing some strings of spaces with TAB
and it still works...
So I don't have to code for varying length of the delimting whitespace.
I wish I had known this many years earlier,,,

--

Bo Berglund
Sweden & Texas
now using XanaNews 1.20-0cfde51
GPG public key: http://tinyurl.com/jbmuutu
Jim Fleming

Posts: 113
Registered: 2/12/00
Re: Splitting strings on whitespace in Delphi?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 23, 2016 8:08 AM   in response to: Bo Berglund in response to: Bo Berglund
Bo Berglund wrote:

Rudy Velthuis (TeamB) wrote:

Bo Berglund wrote:

Is there a quick way to split a string of whitespace separated
values into the separate members?

You could use a TStringList with the proper separators and
delimiters set. Or you use StrUtils.SplitString.

I also asked in the FreePascal mail list and was suggested to use the
DelimitedText property when I add the line to the stringlist.
It actually worked right off for my test files without first trying to
set the delimiter (which would not work since the delimiter is varying
in length). In fact I tested replacing some strings of spaces with TAB
and it still works...
So I don't have to code for varying length of the delimting
whitespace. I wish I had known this many years earlier,,,

Many moons ago I learned that ....

You learn a little more every day . . .

JF
--
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Splitting strings on whitespace in Delphi?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 23, 2016 11:59 AM   in response to: Bo Berglund in response to: Bo Berglund
Bo wrote:

I also asked in the FreePascal mail list and was suggested to use
the DelimitedText property when I add the line to the stringlist.
It actually worked right off for my test files without first trying to
set the delimiter (which would not work since the delimiter is varying
in length). In fact I tested replacing some strings of spaces with
TAB and it still works...
So I don't have to code for varying length of the delimting whitespace.

When the StrictDelimiter property is False (the default), characters #1..#32
are treated as extra whitespace delimiters. If StrictDelimiter is True,
extra whitespace delimiters are ignored.

Regardles of StrictDelimiter, the DelimitedText property setter takes the
Delimiter and QuoteChar properties into account, where the default Delimiter
is #44 (comma) and the default QuoteChar is #34 (double-quote).

If you want to use TStringList to split a string on only whitespace, you
would have to either:

1. set StrictDelimiter to False and set Delimiter to #0.

2. if the whitespace is always a specific character (space, etc), set StrictDelimiter
to True and set Delimiter to that character.

If you don't want quoted text to be unquoted, you would have to set the QuoteChar
to #0.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02