Mega Code Archive

 
Categories / Delphi / Strings
 

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

Title: FirstDelimiter Delphi Function - Returns Index of the First Occurence in a String of the Characters Specified 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 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;