Board index » delphi » Extracting strings

Extracting strings

Dear Peter:
1.  Copy the line to a string - call it OS
2. Set up another string - call it NS
3.  for i := 1 to length(OS) do
     begin
        if copy (OS,i,1) = ',' then break;
       NS + copy (OS,i,1);
     end;

This should do the trick stopping at the comma.

Arrow

Quote
Peter Read wrote in message <36c37439.9393...@news.ozemail.com.au>...
>G'day,

>I have read the following string (stock market data) into a list box
>AAA, 990201, 2.46, 2.5, 2.4, 2.47, 277940

>but only want the first letters up to the comma.  These will be between 3
>and 6 characters long.    How do I do it?

>I can use LoadfromFile to load the entire file, but not just the parts I
>want.
>I have a POS routine that converts the string to their individual data
>pieces but don't know how to add them to the list box.

>Do I have to use a WHILE loop?  If so, how do I determine the number of
>elements in the data file?

>Thanks for your continuing help.

>Regards Peter

 

Re:Extracting strings


G'day,

I have read the following string (stock market data) into a list box
AAA, 990201, 2.46, 2.5, 2.4, 2.47, 277940

but only want the first letters up to the comma.  These will be between 3
and 6 characters long.    How do I do it?

I can use LoadfromFile to load the entire file, but not just the parts I
want.
I have a POS routine that converts the string to their individual data
pieces but don't know how to add them to the list box.

Do I have to use a WHILE loop?  If so, how do I determine the number of
elements in the data file?

Thanks for your continuing help.

Regards Peter

Re:Extracting strings


Quote
Peter Read wrote:
> G'day,

> I have read the following string (stock market data) into a list box
> AAA, 990201, 2.46, 2.5, 2.4, 2.47, 277940

> but only want the first letters up to the comma.  These will be between 3
> and 6 characters long.    How do I do it?

> I can use LoadfromFile to load the entire file, but not just the parts I
> want.
> I have a POS routine that converts the string to their individual data
> pieces but don't know how to add them to the list box.

> Do I have to use a WHILE loop?  If so, how do I determine the number of
> elements in the data file?

> Thanks for your continuing help.

> Regards Peter

Hi Peter,

Off the top of my head I'd use Pos to find the comma and then something like
one of these:

//****************************************************************************

// Return cnt characters from left of str
//****************************************************************************

function LeftStr(const str: String; cnt: Word): String;

begin
 if cnt <= Length(str) then
   Result := Copy(str, 1, cnt);
end;

//****************************************************************************

// Return cnt characters from right of str
//****************************************************************************

function RightStr(const str: String; cnt: Word): String;

begin
 if cnt <= Length(str) then
  Result := Copy(str, Length(str) - cnt + 1, cnt);
end;

//****************************************************************************

// Return cnt characters from str starting at idx
//****************************************************************************

function MidStr(const str: String; idx, cnt: Word): String;

begin
    if (cnt < Length(str)) and (idx < Length(str)) then
     Result := Copy(str, idx, cnt);
end;

to return the bit you need.

HTH

--
************************************************************
Ian Jennings
Microware Data Services

This post is made entirely from recycled ones and noughts
************************************************************

Re:Extracting strings


To extract the first letters upto the comma that can be any length of
3 - 6 characters use this

function Cut(S:String):String;
var s2:string;
begin
s2:=copy(s,1,pos(',',s)-1);
cut:=s2;
end;

that will return anything upto the first coma

hope this helps.

Karl

On Fri, 12 Feb 1999 00:33:46 GMT, p...@ozemail.com.au (Peter Read)
wrote:

Quote
>G'day,

>I have read the following string (stock market data) into a list box
>AAA, 990201, 2.46, 2.5, 2.4, 2.47, 277940

>but only want the first letters up to the comma.  These will be between 3
>and 6 characters long.    How do I do it?

>I can use LoadfromFile to load the entire file, but not just the parts I
>want.
>I have a POS routine that converts the string to their individual data
>pieces but don't know how to add them to the list box.

>Do I have to use a WHILE loop?  If so, how do I determine the number of
>elements in the data file?

>Thanks for your continuing help.

>Regards Peter

Re:Extracting strings


Quote
K...@Rytec.freeserve.co.uk (Shaggy) wrote:

|To extract the first letters upto the comma that can be any length of
|3 - 6 characters use this

|function Cut(S:String):String;
|var s2:string;
|begin
|s2:=copy(s,1,pos(',',s)-1);
|cut:=s2;
|end;

no need for the local variable.  Just:

  Result:=copy(s,1,pos(',',s)-1;

Phil

Re:Extracting strings


function GetOneField(var S: string; const delim: char): string;
begin
    if pos(delim, S) = 1 then
    begin
        delete(s, 1, 1);
        result := '';
    end
    else
    begin
        while pos(delim, S) = 1 do delete(S, 1, length(delim));

        if length(S) > 0 then
        begin
            if pos(delim, S) > 0 then
            begin
                result := copy(S, 1, pos(delim, S)-1);
                delete(S, 1, (pos(delim, S)-1) + length(delim));
            end
            else
            begin
                result := S;
                S := '';
            end;
        end
        else
          result := '';
    end;
end;

And I call it like this

var
    MyString, sHold, OneField: String;
    myIndex: integer;
begin
    myString := 'Abc,Dave,12345.67,200.00';
    sHold := myString;
    myIndex := 0;
    while sHold <> '' do
    begin
        inc( myIndex );
        OneField := GetOneField(sHold, ','); //the "," is the delimiter
        case myIndex of
          1: CompanyString := OneField;
          2: Name := OneField;
          3: Debit := OneField;
          4: Credit := OneField;
         else Raise Exception.Create('Too many fields in string');
        end;
    end;
// handle the values here...
end;

this isn't much  different to the other responses except you can
continue to read the string and out as many case lines in as
the number of fields you may or may not get.

Regards, David

Quote
Peter Read wrote in message <36c37439.9393...@news.ozemail.com.au>...
>G'day,

>I have read the following string (stock market data) into a list box
>AAA, 990201, 2.46, 2.5, 2.4, 2.47, 277940

>but only want the first letters up to the comma.  These will be between 3
>and 6 characters long.    How do I do it?

>I can use LoadfromFile to load the entire file, but not just the parts I
>want.
>I have a POS routine that converts the string to their individual data
>pieces but don't know how to add them to the list box.

>Do I have to use a WHILE loop?  If so, how do I determine the number of
>elements in the data file?

>Thanks for your continuing help.

>Regards Peter

Other Threads