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

x
?
Solved

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

Posted on 2002-07-17
5
Medium Priority
?
394 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 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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