1. Home
  2. Computing & Technology
  3. Delphi Programming

FirstDelimiter Delphi Function
Returns Index of the First Occurence in a String of the Characters Specified

By Zarko Gajic, About.com

Submitted by Jens Borrisholt

In the RTL there's a LastDelimiter function which returns the index of the last occurence in a string of the characters cpecified.

What about the first delimiter - a function that would return the index of the first occurence in a string of the characters cpecified.

FirstDelimiter

Here's the function returning the index of the first character in a string specified by a set of characters - delimiters:
function FirstDelimiter(const Delimiters, S: string): Integer;
var
  P, Q: PChar;
  Len : Integer;
begin
  Result := 0;
  P := Pointer(Delimiters) ;
  Q := Pointer(s) ;
  Len := StrLen(Q) ;
  while Result < Len do
    if (Q[Result] <> #0) and (StrScan(P, Q[Result]) <> nil) then
      Exit
    else
      Inc(Result) ;
end;
Usage example:
//handles Edit1's OnChange event - gets the index of the first number character in a string
procedure TForm1.Edit1Change(Sender: TObject) ;
var
  s : String;
  i : Integer;
begin
 i := FirstDelimiter('0123456789', (Sender as TEdit).Text) ;
 Caption := Copy((Sender as TEdit).Text, 1, i) ;
end;

Delphi tips navigator:
» Delphi Project Structure Folder Organisation - Best Practice in How to Layout Delphi Project Files
« Prevent the System from Entering Sleep or Turning off the Display while your Delphi Application is Running

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2008 Tips
  7. FirstDelimiter Delphi Function - Returns Index of the First Occurence in a String of the Characters Specified

©2009 About.com, a part of The New York Times Company.

All rights reserved.