Solved

"ntword" alternative (parse function from NetMasters PSock unit)

Posted on 2002-07-17
5
367 Views
Last Modified: 2010-04-04
I need a parse function that does the exact same thing as NetMaster's Nthword function as I don't use NetMaster's anymore, but still need this function.  It's usage is like:

nthword(string, delimiter, # of delimiter from beginning of text)

var
  s := string (some kind of text like "2,3,2,3,5,1,2")
begin
  nthword(s, ',', 4);
end;

This will return the text between the 4th and 5th Comma from the beginning.  So the the result would be 5.

Appreciate any help.
Cheers
0
Comment
Question by:woodie
  • 3
5 Comments
 
LVL 5

Accepted Solution

by:
alanwhincup earned 100 total points
ID: 7161018
Something like this should work:

function NthWord(InputString : string; Delimiter : Char; Number : Integer): string;
var
  StrPos, CountSpace : Integer;
  S : string;
begin
  CountSpace := 1;
  StrPos := 1;
  while (CountSpace < Number) and
        (StrPos < Length(InputString)) do
  begin
    if InputString[StrPos] = Delimiter then
      CountSpace := CountSpace + 1;
    Inc(StrPos);
  end;
  if CountSpace = Number then
  begin
    S := '';
    while (InputString[StrPos] <> Delimiter) and
          (StrPos <= Length(InputString)) do
    begin
      S := S + InputString[StrPos];
      StrPos := StrPos + 1;
    end;
    Result := S;
  end
  else
    Result := '';
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(NthWord('2,3,2,3,5,1,2', ',', 4)) ;
end;

Cheers,

Alan
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7162094
?

function nthword(s : string; n : integer) : String;
var sl : Tstringlist;
begin
  result := '';
  sl := tstringlist.create;
  try
    sl.commatext := s;
    try
      result := sl[n];
    except
      //reject outofbounds-exception
    end;
  finally
    sl.free;
  end;
end;
 
just as alternative
and just from head and only for comma

meikl ;-)  
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7162124
or another, supports also different delimiters

//helper function
//it returns the first part as result, and shorten the given string
//for next item simple call again
//if there are no items, then the given string becomes empty
function SplitStr(Var AString : String; ADelim : String) : String;
var APos : Integer;
begin
 Result := '';
 APos := Pos(ADelim,AString);
 if APos > 0 then
 begin
   Result := Copy(AString,1,APos-1);
   AString := copy(Astring,APos+length(Adelim),MaxLongInt);
 end
 else
 begin
   Result := AString;
   AString := '';
 end;
end;

function nthword(s, delim : String; n : integer) : String;
var
  tmps : String;
  i : integer;
begin
  tmps := s;
  for i := 0 to n do
    result := splitstr(tmps,delim);
end;

just from head

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7179523
hello?
0
 

Author Comment

by:woodie
ID: 7180040
Sorry for the delay guys..got a little too busy with work.

Anyway, alanwhincup's solution was exactly what I was looking for.  Plugged his function in place of Ntword function....and works perfect.  Thanks so much..
Cheers!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

17 Experts available now in Live!

Get 1:1 Help Now