Implement StringTokenizer function module in Delphi

zhaozj2021-02-16  56

We know that StringTokenizer in Java is very convenient for string processing, but this feature is provided in Delphi, and I refer to the Delphi version and C # version of this feature in the Java language. I hope to provide you with a convenience. I hope more advice. ZENGQINGSONG@sohu.com

unit SkyStringTokenizer; {string type of analysis: once pine Time: 2004/2/21 (2003 New Year's Eve)}

Interface

uses SysUtils, Classes; type IIterator = interface function hasNext (): boolean; function next (): string; end; type TSkyStringTokenizer = class (TComponent, IIterator) private tokens: TStringList; index: integer; data: string; delimiter: string ; procedure init (dataLine: string; delim: string); function CharacterInTokens (ch: string; const sl: TStringList): boolean; function StringToCharArray (delim: string): TStringList; function SplitString (source, ch: string): Tstringlist;

protected {Protected declarations} public {Public declarations} constructor create (dataLine: string); overload; constructor Create (dataLine: string; delim: string); overload; destructor destroy (); override; function hasNext (): boolean; function next (): String;

Published {Published Declarations} END;

PROCEDURE register;

IMPLEMENTATION

Procedure Register; Begin RegisterComponents ('SkyDevkit', [TskyStringTokenizer]); END;

{TStringTokenizer}

Function TskyStringTokenizer.hasnext: Boolean; Begin Result: = (INDEX

procedure TSkyStringTokenizer.init (dataLine, delim: string); begin tokens: = TStringList.Create (); delimiter: = delim; data: = dataLine; tokens: = SplitString (data, delimiter); // tokens: = data.Split Delimiter.tochararray ()); index: = -1;

Function TskystringTokenizer.next: string; var s: string; begin inc; s: = tokens [index]; while ((LENGTH (S) <= 0) AND (INDEX

function TSkyStringTokenizer.StringToCharArray (delim: string): TStringList; var sl: TStringList; I: Integer; begin sl: = TStringList.Create (); for I: = 1 to length (delim) do // Iterate begin sl.Append ( COPY (Delim, I, 1)); end; // for results: = sl; end;

Constructor TskystringTokenizer.create (Dataline, Delim: String); Beginnherited Create (NIL); Self.init (Dataline, Delim);

Constructor TskyStringTokenizer.create (Dataline: String); Begin Inherited Create (NIL); Self.init (Dataline, '');

Function TskyStringTokenizer.Splitstring: TstringList; var temp: string; // i: integer; sl: tstringlist; index: integer; begin if ch = '' THEN CH: = ';

Sl: = tstringlist.create (); result: = tstringlist.create (); sl: = Self.StringToChararray (CH);

Temp: = '; for index: = 1 to Length (Source) do // ipedness (Source [index], sl) The begin temp: = Temp Source [index]; end else begin result.add (TEMP); TEMP: = ''; end; // if end; // for if (Temp <> ') Then Result.Add (TEMP); SL.FREE; END;

Function TskystringTokenizer.CharacterIntokens (CH: String; Const Sl: TstringList): Boolean; Var index: integer; // i: integer; begin result: = false; for index: = 0 to sl.count - 1 do // iperate begin Result: = Result OR (CH = SL [index]); {if ch = sl [index] The begin result: = true; break; end;} end; // forend; destructor tsky4tringTokenizer.destroy; begin tokens.free; inherited;

End.

// below is a C # version, the initial version is not my written

Using system;

namespace CsharpPats {// String Tokenizer class public class StringTokenizer {private string data, delimiter; private string [] tokens; private int index; public StringTokenizer (string dataLine) {init (dataLine, "");} private void init (String dataLine String delim) {DELIMITER = DELIM; DATA = Dataline; tokens = Data.Split (Delimiter.tocharRay ()); index = 0;} public stringtokenizer (String Dataline, String Delim) {init (Dataline, Delim);} PUBLIC Bool HasmoreElements () {Return (Index <(tokens.length));} public string nextelement () {string s = tokens [index ]; while ((S.Length <= 0) && (Index

转载请注明原文地址:https://www.9cbs.com/read-23109.html

New Post(0)