1. Computing & Technology

Discuss in my forum

FirstDelimiter Delphi Function

Returns Index of the First Occurence in a String of the Characters Specified

By , About.com Guide

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

©2012 About.com. All rights reserved.

A part of The New York Times Company.