Solved

string function

Posted on 2001-09-13
6
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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