Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to split a string when the substrings are separated by more than one space c Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
18-Oct-02
Category
Object Pascal-Strings
Language
Delphi 2.x
Views
99
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I have a lot of the following lines:

02-07-01 12:05:30  XXX     AAAAAA 100 BBBBB   666666        300
3700     -555.00     4.00

The only way to separate those items in the string is if there are more than 2 
spaces between the items. There can also be 3 to 5 spaces actually. If there is one 
space between strings - it is probably part of the bigger item as shown above: 
'AAAAAA 100 BBBBB'. What would be the simplest way to split this string? I looked 
at delimitedText but I am not sure of if it is going to help me.

Answer:

Solve 1:

See routine below. Is this data you have in a fixed-width column format produced by 
another program? If so you cannot count on fields being separated by more than one 
space! In fact you may have cases where there is no space between the fields 
because a value fills the whole field width! For such files you have to use a 
different strategy to parse the lines.

1   {SplitDataString:
2   Dissect a string of items separated by more than one space character
3   Param S contains the string to split, param list takes the items obtained from S
4   Precondition: list <> nil
5   
6   Description:
7   An item cannot start or end with a space but it may contain space characters flanked
8   by non-space characters. The routine does not support multibyte character sets as it
9   is implemented now.
10  
11  Created 28.7.2002 by P. Below}
12  
13  procedure SplitDataString(S: string; list: TStrings);
14  var
15    startindex: Integer;
16  
17    function HasNextItem: Boolean;
18    begin
19      {We do not support a "Item" starting with a space!}
20      while (startindex <= Length(S)) and (S[startindex] = #32) do
21        Inc(startindex);
22      Result := startindex <= Length(S);
23    end;
24  
25    function GetNextItem: string;
26    var
27      endindex: Integer;
28    begin
29      for endindex := startindex + 1 to Length(S) do
30      begin
31        if S[endindex] = ' ' then
32          if S[endindex + 1] = ' ' then
33          begin
34            {found end of a Item}
35            Result := Copy(S, startindex, endindex - startindex);
36            startindex := endindex + 2;
37            Exit;
38          end;
39      end;
40      {If we end here Item is the last in S}
41      Result := Copy(S, startindex, maxint);
42      startindex := Length(S) + 1;
43    end;
44  
45  begin
46    Assert(Assigned(list));
47    {remove whitespace from start and end of string}
48    S := Trim(S);
49    startindex := 1;
50    while HasNextItem do
51      list.Add(GetNextItem);
52  end;
53  
54  Example of use:
55  
56  procedure TForm1.Button1Click(Sender: TObject);
57  begin
58    memo1.clear;
59    SplitDataString('02-07-01 12:05:30  XXX     AAAAAA 100 BBBBB   666666        300  
60    					'      -555.00     4.00', memo1.lines);
61  end;
62  
63  
64  Solve 2:
65  
66  function SepSpacedOutStr(s: string): string;
67  var
68    i, x: integer;
69  begin
70    s := SysUtils.Trim(s);
71    if s <> '
72    begin
73      SetLength(result, Length(s));
74      x := 0;
75      i := 1;
76      while i <= Length(s) do
77      begin
78        if (s[i] <> #32) or ((i < Length(s)) and (s[i + 1] <> #32)) then
79        begin
80          Inc(x);
81          result[x] := s[i];
82        end
83        else
84        begin
85          if (i < Length(s)) and (s[i + 1] = #32) then
86          begin
87            Inc(x);
88            result[x] := ',';
89            Inc(x);
90            result[x] := #32;
91            while (i < Length(s)) and (s[i + 1] = #32) do
92              Inc(i);
93          end;
94        end;
95        Inc(i);
96      end;
97      SetLength(result, x);
98    end
99    else
100     result := '';
101 end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC