Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

string function

Posted on 2001-09-13
6
Medium Priority
?
208 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 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

604 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