Solved

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

Posted on 2002-07-17
5
370 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

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

20 Experts available now in Live!

Get 1:1 Help Now