Link to home
Start Free TrialLog in
Avatar of msl22
msl22

asked on

string function

Hi,

I have a string given by the user.
This string contains something like this:
 Agency := 'Fire, Police, EMS'.

Now, I have to check if the user can use each of those words. To to this I have a list of string containing all the words the user can access.

My question, how can I get each word contained in the string Agency ?
I know that they must be separated by a coma.

Thank you.

Mary
ASKER CERTIFIED SOLUTION
Avatar of TOndrej
TOndrej

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This function will break any string(BaseString) given a separator string(Breakstring)!

If u want a usage sample for this function download the sample from this link:
http://www.victory.hpg.com.br/Samples/StringQuebra.zip


function sBreakApart(BaseString, BreakString: string;StringList:TStringList): TStringList;
var
EndOfCurrentString: byte;
TempStr: string;
begin
repeat
EndOfCurrentString := Pos(BreakString, BaseString);
if EndOfCurrentString = 0 then
StringList.add(BaseString)
else
StringList.add(Copy(BaseString, 1, EndOfCurrentString - 1));
BaseString := Copy(BaseString, EndOfCurrentString +
length(BreakString), length(BaseString) - EndOfCurrentString);

until EndOfCurrentString = 0;
result := StringList;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
t: TStringList;
begin
t := TStringList.create;
ListBox1.Items.Assign(sBreakApart(Edit1.Text, ',', t));
t.free;
end;

Hope this helps!
VSF
www.victory.hpg.com.br
Avatar of alsantos
alsantos

Hi Mary, try this function to get the word you want.

function TForm1.BreakParams(Str, Separator: String; Ind: Integer): String;
var
i, ii, StartPos, EndPos: Integer;
begin
ii:=1;
EndPos:=0;
StartPos:=0;
for i:=1 to length(Str) do
begin
  if Str[i] = Separator[1] then Inc(ii);
  if ii > Ind then
  begin
    EndPos:=i;
    Break;
  end
  else if ((ii = Ind) and (StartPos = 0)) then StartPos:=i;
end;
if ind > 1 then Inc(StartPos);
if StartPos <> 0 then
begin
  if (EndPos = 0) then EndPos:=Length(Str) +1;
  BreakParams:=Copy(str, StartPos, (EndPos-StartPos));
end;
end;

Now, if you wanna get the first word try this:
edit1.text := breakparams(Agency, ',', 1);
if you wanna get the second word try this:
edit2.text := breakparams(Agency, ',', 2);
...

If you don't know how words the string contains, do this when you call the function:

procedure TForm1.Button1Click(Sender: TObject);
var
  x: integer;
begin
x:=1;
while edit1.text <> agency do
begin;
  if breakparams(agency, ',', x) = edit1.text then exit;
  edit.text:=edit1.text + breakparams(agency, ',', x);
  { you'll put the string now separated by space, so use this example to do what you want }
  inc(x);
end;
end;

Alexandre Santos
Hello, there is a little demo program:

procedure TForm1.FormCreate(Sender: TObject);
begin
  List := TStringList.Create;
  List.Add('Fire');
  List.Add('Police');
  List.Add('EMS');
  List.Sorted := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  CheckWords('Fire, Police, EMS');
end;

procedure TForm1.CheckWords(S: string);
var
  P: integer;
  NextWord, SubStr: string;
begin
  SubStr := S + ',';
  while SubStr <> '' do begin
    P := Pos(',',SubStr);
    NextWord := Trim(Copy(SubStr, 1, P - 1));
    SubStr  := Trim(Copy(SubStr, P + 1, Length(SubStr) - P));
    // Compare words
    if List.IndexOf(NextWord) < 0  then
      MessageDlg(format('Word "%s" not found!',[NextWord]),mtError,[mbOk],0)
    else
      MessageDlg(format('Word "%s" found!',[NextWord]),mtInformation,[mbOk],0);
  end;
end;

Alexander
I use this little function I wrote to extract a certain value from a comma delimited string. Simply pass in field number to extract and the whole string, the result is the value. It deals with quote-delimited characters as well, like a standard CSV file.

function GetCommaValues(Field: Integer; MsgIn: String): String;
// This function extracts the information in the incoming comma-separated
// message after the comma number specified by "Field".
Var
   CommaCount: ShortInt;
   iPos: Integer;
   InQuotes: Boolean;
   s: String;
Begin
     // Initialise.
     CommaCount := 1;
     iPos := 1;
     Result := '';
     InQuotes := False;

     While CommaCount <> Field Do
     Begin
          s := Copy(MsgIn, iPos, 1);

          While ((s <> ',') Or (InQuotes)) And (iPos <= Length(MsgIn)) Do
          Begin
               If (s = '"') then InQuotes := Not (InQuotes);
               inc(iPos);
               s := Copy(MsgIn, iPos, 1);
          End;

          inc(CommaCount);
          inc(iPos);
     End;

     s := Copy(MsgIn, iPos, 1);
     While ((s <> ',') Or (InQuotes))
           Or (iPos = Length(MsgIn) + 1) Do
     Begin
           If (s = '"') then InQuotes := Not (InQuotes);
           Result := Result + s;

           If (iPos <= Length(MsgIn) + 1) then
           Begin
                inc(iPos);
                s := Copy(MsgIn, iPos, 1);
           End

           Else Break;
     End;

     Result := RemoveChar(Result,'"');
     Result := Trim(Result);    
End;

J.
Avatar of msl22

ASKER

Hi,

Thank you for all your answers.
But for the work I have to do, this one was the faster one to use.

But I'll keep in memory all your function... I'll probably use them in the future.

Thank you !

Mary