Solved

string function

Posted on 2001-09-13
6
190 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

16 Experts available now in Live!

Get 1:1 Help Now