Solved

string function

Posted on 2001-09-13
6
189 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:msl22
6 Comments
 
LVL 8

Accepted Solution

by:
TOndrej earned 50 total points
ID: 6479656
Hi,

you can use TStringList.CommaText for this purpose, e.g.

var
  List: TStringList;
begin
  List := TStringList.Create;
  try
    List.CommaText := 'Fire, Police, EMS';
    for I := 0 to List.Count - 1 do
      ShowMessage('"' + List[I] + '"');
  finally
    List.Free;
  end;
end;
0
 
LVL 3

Expert Comment

by:VSF
ID: 6480776
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
0
 
LVL 2

Expert Comment

by:alsantos
ID: 6481236
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
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Expert Comment

by:bartsev
ID: 6482143
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
0
 
LVL 4

Expert Comment

by:jsweby
ID: 6482209
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.
0
 

Author Comment

by:msl22
ID: 6488125
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
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now