Delphi XE3增加了字符串的辅助功能.可以像操作一个结构体或者类一样操作字符串了.也就是给字符串追加了成员方法.
procedure TForm5.Button1Click(Sender: TObject);
begin
Caption := ‘abc’.ToUpper; //大写ABC
Caption := string.create(‘a’,20);? //20个a
Caption := Inttostr(‘哈哈’.Length);
end;
看上去很神奇吧.
在sysutils单元
TStringSplitOptions = (None, ExcludeEmpty);
TStringHelper = record helper for string
private
function GetChars(Index: Integer): Char; inline;
function GetLength: Integer; inline;
class function CharInArray(const C: Char; const InArray: array of Char): Boolean; static;
function IndexOfAny(const Values: array of string; var Index: Integer): Integer; overload;
public
const Empty = ”;
// Methods
class function Create(C: Char; Count: Integer): string; overload; inline; static;
class function Create(const Value: array of Char; StartIndex: Integer; Length: Integer): string; overload; static;
class function Create(const Value: array of Char): string; overload; static;
class function Compare(const StrA: string; const StrB: string): Integer; overload; static;
class function Compare(const StrA: string; const StrB: string; IgnoreCase: Boolean): Integer; overload; static;
class function Compare(const StrA: string; IndexA: Integer; const StrB: string; IndexB: Integer; Length: Integer): Integer; overload; static;
class function Compare(const StrA: string; IndexA: Integer; const StrB: string; IndexB: Integer; Length: Integer; IgnoreCase: Boolean): Integer; overload; static;
class function CompareOrdinal(const strA: string; const strB: string): Integer; overload; static;
class function CompareOrdinal(const strA: string; indexA: Integer; const strB: string; indexB: Integer; length: Integer): Integer; overload; static;
function CompareTo(const strB: string): Integer;
function Contains(const Value: string): Boolean;
class function Copy(const Str: string): string; inline; static;
procedure CopyTo(SourceIndex: Integer; var destination: array of Char; DestinationIndex: Integer; Count: Integer);
class function EndsText(const ASubText, AText: string): Boolean; static;
function EndsWith(const Value: string): Boolean; overload;
function EndsWith(const Value: string; IgnoreCase: Boolean): Boolean; overload;
function Equals(const Value: string): Boolean; overload;
class function Equals(const a: string; const b: string): Boolean; overload; static;
class function Format(const Format: string; const args: array of const): string; overload; static;
function GetHashCode: Integer;
function IndexOf(value: Char): Integer; overload; inline;
function IndexOf(const Value: string): Integer; overload; inline;
function IndexOf(Value: Char; StartIndex: Integer): Integer; overload;
function IndexOf(const Value: string; StartIndex: Integer): Integer; overload;
function IndexOf(Value: Char; StartIndex: Integer; Count: Integer): Integer; overload;
function IndexOf(const Value: string; StartIndex: Integer; Count: Integer): Integer; overload;
function IndexOfAny(const AnyOf: array of Char): Integer; overload;
function IndexOfAny(const AnyOf: array of Char; StartIndex: Integer): Integer; overload;
function IndexOfAny(const AnyOf: array of Char; StartIndex: Integer; Count: Integer): Integer; overload;
function Insert(StartIndex: Integer; const Value: string): string;
//??? function IsNormalized: Boolean; overload;
//??? function IsNormalized(NormalizationForm: TNormalizationForm): Boolean; overload;
function IsDelimiter(const Delimiters: string; Index: Integer): Boolean;
function IsEmpty: Boolean;
class function IsNullOrEmpty(const Value: string): Boolean; static;
class function IsNullOrWhiteSpace(const Value: string): Boolean; static;
class function Join(const Separator: string; const values: array of const): string; overload; static;
class function Join(const Separator: string; const Values: array of string): string; overload; static;
//??? class function Join<T>(Separator: string; const values: IEnumerable<T>): string; overload; static;
class function Join(const Separator: string; const Values: IEnumerable<string>): string; overload; static;
class function Join(const Separator: string; const value: array of string; StartIndex: Integer; Count: Integer): string; overload; static;
function LastDelimiter(const Delims: string): Integer;
function LastIndexOf(Value: Char): Integer; overload;
function LastIndexOf(const Value: string): Integer; overload;
function LastIndexOf(Value: Char; StartIndex: Integer): Integer; overload;
function LastIndexOf(const Value: string; StartIndex: Integer): Integer; overload;
function LastIndexOf(Value: Char; StartIndex: Integer; Count: Integer): Integer; overload;
function LastIndexOf(const Value: string; StartIndex: Integer; Count: Integer): Integer; overload;
function LastIndexOfAny(const AnyOf: array of Char): Integer; overload;
function LastIndexOfAny(const AnyOf: array of Char; StartIndex: Integer): Integer; overload;
function LastIndexOfAny(const AnyOf: array of Char; StartIndex: Integer; Count: Integer): Integer; overload;
function PadLeft(TotalWidth: Integer): string; overload; inline;
function PadLeft(TotalWidth: Integer; PaddingChar: Char): string; overload; inline;
function PadRight(TotalWidth: Integer): string; overload; inline;
function PadRight(TotalWidth: Integer; PaddingChar: Char): string; overload; inline;
function Remove(StartIndex: Integer): string; overload; inline;
function Remove(StartIndex: Integer; Count: Integer): string; overload; inline;
function Replace(OldChar: Char; NewChar: Char): string; overload;
function Replace(OldChar: Char; NewChar: Char; ReplaceFlags: TReplaceFlags): string; overload;
function Replace(const OldValue: string; const NewValue: string): string; overload;
function Replace(const OldValue: string; const NewValue: string; ReplaceFlags: TReplaceFlags): string; overload;
function Split(const Separator: array of Char): TArray<string>; overload;
function Split(const Separator: array of Char; Count: Integer): TArray<string>; overload;
function Split(const Separator: array of Char; Options: TStringSplitOptions): TArray<string>; overload;
function Split(const Separator: array of string; Options: TStringSplitOptions): TArray<string>; overload;
function Split(const Separator: array of Char; Count: Integer; Options: TStringSplitOptions): TArray<string>; overload;
function Split(const Separator: array of string; Count: Integer; Options: TStringSplitOptions): TArray<string>; overload;
function StartsWith(const Value: string): Boolean; overload;
function StartsWith(const Value: string; IgnoreCase: Boolean): Boolean; overload;
function Substring(StartIndex: Integer): string; overload;
function Substring(StartIndex: Integer; Length: Integer): string; overload;
function ToCharArray: TArray<Char>; overload;
function ToCharArray(StartIndex: Integer; Length: Integer): TArray<Char>; overload;
function ToLower: string;
function ToLowerInvariant: string;
function ToUpper: string;
function ToUpperInvariant: string;
function Trim: string; overload;
function Trim(const TrimChars: array of Char): string; overload;
function TrimEnd(const TrimChars: array of Char): string;
function TrimStart(const TrimChars: array of Char): string;
property Chars[Index: Integer]: Char read GetChars;
property Length: Integer read GetLength;
end;
看上去挺好的.利用了Delphi2005以来增加的helper,给类或者结构体打补丁的功能.
Helper实际编译后不会产生class/record之类的概念,基本不占内存.调用方法也只不过是编译成普通方法,追加第一个参数是具体helper所辅助的类/结构体的实例.
但是易博龙集成了Borland一贯做事情不彻底的情况.这个功能非常好,但会留下一点点小遗憾.
因为这个Helper本质是对system单元的字符串操作函数的调用的转接,前面很多方法是inline的,也就是编译时会展开,变成直接调用system字符串相关的函数.和我们手工调用没什么区别.但不知道为什么后面的一些方法又没用了inline.这样相当多了一次call/ret的开销.效率会有一点点影响.