Solved

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

Posted on 2002-07-17
5
381 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
[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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 388
Magic Software info 18 151
Delphi Yen format 3 70
Drag & Drop... Data from one grid to another 2 51
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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