在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
end;
-------------------------------------------------------------------------------- 适宽处理: -------------------------------------------------------------------------------- function PadLeft(TotalWidth: Integer): string; function PadLeft(TotalWidth: Integer; PaddingChar: Char): string; function PadRight(TotalWidth: Integer): string; function PadRight(TotalWidth: Integer; PaddingChar: Char): string; //-------------------------------------------------------------------------------- var str: string; begin str := '1'; str := str.PadLeft(4, '0'); // 0001 end; -------------------------------------------------------------------------------- 插入与删除: -------------------------------------------------------------------------------- function Insert(StartIndex: Integer; const Value: string): string; function Remove(StartIndex: Integer): string; function Remove(StartIndex: Integer; Count: Integer): string; //-------------------------------------------------------------------------------- var str1, str2: string; begin str1 := 'Delphi 4'; str2 := str1.Insert(7, 'XE'); // Delphi XE4 str1 := 'Delphi XE4'; str2 := str1.Remove(6); // Delphi str2 := str1.Remove(7, 2); // Delphi 4 end; -------------------------------------------------------------------------------- 截取: -------------------------------------------------------------------------------- function Substring(StartIndex: Integer): string; function Substring(StartIndex: Integer; Length: Integer): string; //-------------------------------------------------------------------------------- var str1, str2: string; begin str1 := 'Delphi XE4'; str2 := str1.Substring(7); // XE4 str2 := str1.Substring(7, 2); // XE end; -------------------------------------------------------------------------------- 替换: -------------------------------------------------------------------------------- function Replace(OldChar: Char; NewChar: Char): string; function Replace(OldChar: Char; NewChar: Char; ReplaceFlags: TReplaceFlags): string; function Replace(const OldValue: string; const NewValue: string): string; function Replace(const OldValue: string; const NewValue: string; ReplaceFlags: TReplaceFlags): string; //-------------------------------------------------------------------------------- var str1, str2: string; begin str1 := 'ABC ABC ABC'; str2 := str1.Replace('A', '*'); // *BC *BC *BC str2 := str1.Replace('A', '*', [rfIgnoreCase]); // *BC ABC ABC end; -------------------------------------------------------------------------------- 分割: -------------------------------------------------------------------------------- function Split(const Separator: array of Char): TArray; function Split(const Separator: array of Char; Count: Integer): TArray; function Split(const Separator: array of Char; Options: TStringSplitOptions): TArray; function Split(const Separator: array of string; Options: TStringSplitOptions): TArray; function Split(const Separator: array of Char; Count: Integer; Options: TStringSplitOptions): TArray; function Split(const Separator: array of string; Count: Integer; Options: TStringSplitOptions): TArray; //-------------------------------------------------------------------------------- var str: string; arr: TArray; begin str := 'A-1,B-2,,,C-3,D-4'; arr := str.Split([',']); // arr[0] = A-1; Length(arr) = 6 arr := str.Split([','], TStringSplitOptions.ExcludeEmpty); // 忽略空项; Length(arr) = 4 arr := str.Split([','], 2); // 只提取前 2 arr := str.Split([',', '-'], ExcludeEmpty); //arr[0] = A; Length(arr) = 8 arr := str.Split([',,,'], None); // 分隔符可以是一个字符串数组 end; -------------------------------------------------------------------------------- 连接: -------------------------------------------------------------------------------- class function Join(const Separator: string; const values: array of const): string; class function Join(const Separator: string; const Values: array of string): string; class function Join(const Separator: string; const Values: IEnumerator): string; class function Join(const Separator: string; const Values: IEnumerable): string; class function Join(const Separator: string; const value: array of string; StartIndex: Integer; Count: Integer): string; //-------------------------------------------------------------------------------- var S: string; str: string; strArr: TArray; begin str := 'A1,B2,C3,,,,D4,E5,F6,G7'; strArr := str.Split([','], ExcludeEmpty); str := S.Join('-', strArr); // A1-B2-C3-D4-E5-F6-G7 str := S.Join('; ', [1,2,3,4,5]); // 1; 2; 3; 4; 5 str := S.Join(',', ['abc', 123, true]); // abc,123,True end; -------------------------------------------------------------------------------- 类型转换: -------------------------------------------------------------------------------- function ToBoolean: Boolean; function ToInteger: Integer; function ToSingle: Single; function ToDouble: Double; function ToExtended: Extended; class function ToBoolean(const S: string): Boolean; class function ToInteger(const S: string): Integer; class function ToSingle(const S: string): Single; class function ToDouble(const S: string): Double; class function ToExtended(const S: string): Extended; class function Parse(const Value: Integer): string; class function Parse(const Value: Int64): string; class function Parse(const Value: Boolean): string; class function Parse(const Value: Extended): string; //-------------------------------------------------------------------------------- var S: string; str: string; n: Integer; b: Boolean; f: Double; begin str := S.Parse(123); n := str.ToInteger; // 123 b := str.ToBoolean; // True str := S.Parse(True); b := str.ToBoolean; // True n := str.ToInteger; // -1 str := S.Parse(3.14159260000); f := str.ToDouble; //3.1415926 end; -------------------------------------------------------------------------------- 定界符: -------------------------------------------------------------------------------- function IsDelimiter(const Delimiters: string; Index: Integer): Boolean; function LastDelimiter(const Delims: string): Integer; //-------------------------------------------------------------------------------- var str: string; b: Boolean; n: Integer; begin str := 'http://del.cnblogs.com'; b := str.IsDelimiter(':', 4); // True b := str.IsDelimiter('//', 5); // True n := str.LastDelimiter('.'); // 18 n := str.IndexOf('.'); // 10 end; -------------------------------------------------------------------------------- 空字符串: -------------------------------------------------------------------------------- const Empty = ''; function IsEmpty: Boolean; class function IsNullOrEmpty(const Value: string): Boolean; class function IsNullOrWhiteSpace(const Value: string): Boolean; //-------------------------------------------------------------------------------- var S: string; str: string; b: Boolean; begin str := ' '; b := str.IsEmpty; // False b := S.IsNullOrWhiteSpace(str); // True end; -------------------------------------------------------------------------------- String 与 Char: -------------------------------------------------------------------------------- class function Create(C: Char; Count: Integer): string; class function Create(const Value: array of Char; StartIndex: Integer; Length: Integer): string; class function Create(const Value: array of Char): string; property Chars[Index: Integer]: Char read GetChars; property Length: Integer read GetLength; function CountChar(const C: Char): Integer; function ToCharArray: TArray; function ToCharArray(StartIndex: Integer; Length: Integer): TArray; procedure CopyTo(SourceIndex: Integer; var destination: array of Char; DestinationIndex: Integer; Count: Integer); //-------------------------------------------------------------------------------- var S: string; str, str2: string; charArr: TCharArray; n: Integer; c: Char; begin str := 'ABC'; n := str.Length; // 3 c := str.Chars[0]; // A = str[1] str := S.Create('A', 7); // AAAAAAA charArr := 'ABCDEFG'.ToCharArray; str := s.Create(charArr); // ABCDEFG str := S.Create(charArr, 1, 3); // BCD charArr := '1234567890'.ToCharArray; str := 'ABCDEFG'; str.CopyTo(1, charArr, 2, 3); str := S.Create(charArr); // 12BCD67890 end; -------------------------------------------------------------------------------- 其他: -------------------------------------------------------------------------------- function Equals(const Value: string): Boolean; function GetHashCode: Integer; class function Equals(const a: string; const b: string): Boolean; class function Format(const Format: string; const args: array of const): string; class function Copy(const Str: string): string; //-------------------------------------------------------------------------------- // 用 Equals 不如直接用 = 号 // 用 Copy 不如直接用 := // 用 string.Format 不如直接用 Format() // 总之, 还是有用处的多! 1 喜欢 0 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论