Ignore:
Timestamp:
Mar 26, 2007, 8:34:29 PM (18 years ago)
Author:
RBRi
Message:

improved StringUtilsUnit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/NewView/StringUtilsUnit.pas

    r82 r102  
    1616  StrLF = chr(10);
    1717  StrCRLF = StrCR + StrLF;
     18  Quote = '''';
     19  DoubleQuote = '"';
    1820
    1921
     
    4345  // an optional escape character usable to escape field delimits.
    4446  // Example:
    45   //     StrExtractStrings('1x2x3\x4', "x", '\') ->
    46   //     returns 4 strings: "1", "", "2" and "3x4"
     47  //     StrExtractStrings('1x2x3\x4', 'x', '\') ->
     48  //     returns 4 strings: '1', '', '2' and '3x4'
    4749  Procedure StrExtractStrings(Var aResult : TStrings; Const aReceiver: String; const aSetOfChars: TSetOfChars; const anEscapeChar: char);
    4850
     
    6870  Function StrLeft(const aString : String; const aCount : Integer) : String;
    6971
     72  // Returns a copy of the string without aCount chars from right
     73  Function StrLeftWithout(const aString : String; const aCount : Integer) : String;
     74
     75  // Returns a copy of the string including all characters until one from aSetOfChars found
     76  Function StrLeftUntil(const aReceiver: String; const aSetOfChars: TSetOfChars) : String;
     77
     78  // returns true if the String starts with the provides one
     79  // this is case SENSITIVE
     80  Function StrStartsWith(const aReceiver: String; const aStartString: String): Boolean;
     81
     82  // returns true if the String starts with the provides one
     83  // this is case INsensitive
     84  Function StrStartsWithIgnoringCase(const aReceiver: String; const aStartString: String): Boolean;
     85
    7086  // returns true if the String ends with the provides one
    7187  // this is case SENSITIVE
     
    7490  // returns true if the String ends with the provides one
    7591  // this is case INsensitive
    76   Function StrEndsWithIgnoringCase(const aString: String; const anEndString: String): Boolean;
     92  Function StrEndsWithIgnoringCase(const aReceiver: String; const anEndString: String): Boolean;
    7793
    7894  // the IntToStr generates wrong results
     
    8298
    8399  Function BoolToStr(const aBoolean : boolean ): string;
     100
     101  // Returns aString enclosed in double quotes
     102  Function StrInDoubleQuotes(const aString : String) : String;
    84103
    85104
     
    350369
    351370
     371  Function StrLeftWithout(const aString : String; const aCount : Integer) : String;
     372  Begin
     373    Result:= copy(aString, 1, length(aString) - aCount );
     374  End;
     375
     376
     377  Function StrLeftUntil(const aReceiver: String; const aSetOfChars: TSetOfChars) : String;
     378  Var
     379    i : integer;
     380  Begin
     381    Result := aReceiver;
     382
     383    for i := 1 To Length(aReceiver) do
     384    begin
     385      if aReceiver[i] in aSetOfChars then
     386      begin
     387        Result := Copy(aReceiver, 1, i-1 );
     388        break;
     389      end;
     390    end;
     391  end;
     392
     393
     394  Function StrStartsWith(const aReceiver: String; const aStartString: String) : Boolean;
     395  Var
     396    tmpStringPos : integer;
     397    tmpStartStringLength : integer;
     398  Begin
     399    tmpStartStringLength := Length(aStartString);
     400
     401    if Length(aReceiver) < tmpStartStringLength then
     402    begin
     403      result := false;
     404      exit;
     405    end;
     406
     407    for tmpStringPos := 1 to tmpStartStringLength do
     408    begin
     409      if aReceiver[tmpStringPos] <> aStartString[tmpStringPos] then
     410      begin
     411        result := false;
     412        exit;
     413      end;
     414    end;
     415
     416    result := true;
     417  end;
     418
     419
     420  Function StrStartsWithIgnoringCase(const aReceiver: String; const aStartString: String) : Boolean;
     421  Var
     422    tmpStringPos : integer;
     423    tmpStartStringLength : integer;
     424  Begin
     425    tmpStartStringLength := Length(aStartString);
     426
     427    if Length(aReceiver) < tmpStartStringLength then
     428    begin
     429      result := false;
     430      exit;
     431    end;
     432
     433    for tmpStringPos := 1 to tmpStartStringLength do
     434    begin
     435      if UpCase(aReceiver[tmpStringPos]) <> UpCase(aStartString[tmpStringPos]) then
     436      begin
     437        result := false;
     438        exit;
     439      end;
     440    end;
     441
     442    result := true;
     443  end;
     444
     445
    352446  Function StrEndsWith(const aReceiver: String; const anEndString: String): Boolean;
    353447  Var
     
    384478    tmpMatchPos : Longint;
    385479  Begin
    386     tmpStringPos := length(aString);
     480    tmpStringPos := length(aReceiver);
    387481    tmpMatchPos := length(anEndString);
    388482
     
    395489    while tmpMatchPos > 0 do
    396490    begin
    397       if upcase(aString[tmpStringPos]) <> upcase(anEndString[tmpMatchPos]) then
     491      if upcase(aReceiver[tmpStringPos]) <> upcase(anEndString[tmpMatchPos]) then
    398492      begin
    399493        result := false;
     
    406500    result := true;
    407501  end;
     502
    408503
    409504  Function LongWordToStr(const aLongWord: LongWord) : String;
     
    450545  end;
    451546
     547  Function StrInDoubleQuotes(const aString : String) : String;
     548  begin
     549    Result := DoubleQuote + aString + DoubleQuote;
     550  end;
     551
     552
    452553END.
Note: See TracChangeset for help on using the changeset viewer.