?
Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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