Delphi(lazarus) TStringHelper用法详解(转载)
<div class="articalTitle"><h2 id="t_44fa172f0101n4rh" class="titName SG_txta">Delphi(lazarus) TStringHelper用法详解</h2>
</div>
<div id="sina_keyword_ad_area" class="articalTag">Delphi XE4的TStringHelper,对操作字符串进一步带来更多的方法,使用这些方法才可以实现跨平台的代码。</div>
<div id="sina_keyword_ad_area2" class="articalContent ">Delphi引用单元:<br><span style="color: rgba(255, 0, 0, 1)">System.SysUtils.TStringHelper</span><br>Lazarus引用单元:<br><span style="color: rgba(255, 0, 0, 1)">SysUtils</span></div>
<div class="articalContent "><br><strong>大小写转换:</strong><br>--------------------------------------------------------------------------------<br> function ToLower: string;<br>function ToLower(LocaleID: TLocaleID): string;<br>function ToLowerInvariant: string;<br>function ToUpper: string;<br>function ToUpper(LocaleID: TLocaleID): string;<br>function ToUpperInvariant: string;<br><br>class function LowerCase(const S: string): string;<br>class function LowerCase(const S: string; LocaleOptions: TLocaleOptions): string;<br>class function UpperCase(const S: string): string;<br>class function UpperCase(const S: string; LocaleOptions: TLocaleOptions): string;<br>//--------------------------------------------------------------------------------<br>var<br> str: string;<br>begin<br> str := 'Delphi';<br> str := str.ToLower; // delphi<br> str := str.ToUpper; // DELPHI<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>清除两边空格或指定字符:</strong><br>--------------------------------------------------------------------------------<br> function Trim: string;<br>function TrimLeft: string;<br>function TrimRight: string;<br>function Trim(const TrimChars: array of Char): string;<br>function TrimLeft(const TrimChars: array of Char): string;<br>function TrimRight(const TrimChars: array of Char): string;<br>//--------------------------------------------------------------------------------<br>var<br> str1, str2: string;<br>begin<br> str1 := ' Delphi 10000 ';<br><br> str2 := str1.TrimLeft; // 'Delphi 10000 '<br> str2 := str1.TrimRight; // ' Delphi 10000'<br> str2 := str1.Trim; // 'Delphi 10000'<br><br> str2 := str1.Trim([' ', '0']); // 'Delphi 1'<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>字符串对比:</strong><br>--------------------------------------------------------------------------------<br>function CompareTo(const strB: string): Integer;<br>class function Compare(const StrA: string; const StrB: string): Integer;<br>class function CompareText(const StrA: string; const StrB: string): Integer;<br>class function Compare(const StrA: string; const StrB: string; LocaleID: TLocaleID): Integer;<br>class function Compare(const StrA: string; const StrB: string; IgnoreCase: Boolean): Integer;<br>class function Compare(const StrA: string; const StrB: string; IgnoreCase: Boolean; LocaleID: TLocaleID): Integer;<br>class function Compare(const StrA: string; IndexA: Integer; const StrB: string; IndexB: Integer; Length: Integer): Integer;<br>class function Compare(const StrA: string; IndexA: Integer; const StrB: string; IndexB: Integer; Length: Integer; LocaleID: TLocaleID): Integer;<br>class function Compare(const StrA: string; IndexA: Integer; const StrB: string; IndexB: Integer; Length: Integer; IgnoreCase: Boolean): Integer;<br>class function Compare(const StrA: string; IndexA: Integer; const StrB: string; IndexB: Integer; Length: Integer; IgnoreCase: Boolean; LocaleID: TLocaleID): Integer;<br>class function CompareOrdinal(const StrA: string; const StrB: string): Integer;<br>class function CompareOrdinal(const StrA: string; IndexA: Integer; const StrB: string; IndexB: Integer; Length: Integer): Integer;<br>//--------------------------------------------------------------------------------<br>var<br> str1, str2: string;<br> n: Integer;<br>begin<br> str1 := 'ABC 123';<br> str2 := 'abc 123';<br><br> n := str1.CompareTo(str2); // -32<br><br> n := str1.Compare(str1, str2); // 1<br> n := str1.CompareText(str1, str2); // 0; 相同<br><br> n := str1.Compare(str1, str2, True); // 0; 不区分大小写<br> n := str1.CompareOrdinal(str1, str2); // -32<br><br> n := str1.Compare(str1, 4, str2, 4, 3); // 0; 只对比后三位<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>搜索字符串:</strong><br>--------------------------------------------------------------------------------<br>function IndexOf(value: Char): Integer;<br>function IndexOf(const Value: string): Integer;<br>function IndexOf(Value: Char; StartIndex: Integer): Integer;<br>function IndexOf(const Value: string; StartIndex: Integer): Integer;<br>function IndexOf(Value: Char; StartIndex: Integer; Count: Integer): Integer;<br>function IndexOf(const Value: string; StartIndex: Integer; Count: Integer): Integer;<br>function IndexOfAny(const AnyOf: array of Char): Integer;<br>function IndexOfAny(const AnyOf: array of Char; StartIndex: Integer): Integer;<br>function IndexOfAny(const AnyOf: array of Char; StartIndex: Integer; Count: Integer): Integer;<br><br>function LastIndexOf(Value: Char): Integer;<br>function LastIndexOf(const Value: string): Integer;<br>function LastIndexOf(Value: Char; StartIndex: Integer): Integer;<br>function LastIndexOf(const Value: string; StartIndex: Integer): Integer;<br>function LastIndexOf(Value: Char; StartIndex: Integer; Count: Integer): Integer;<br>function LastIndexOf(const Value: string; StartIndex: Integer; Count: Integer): Integer;<br>function LastIndexOfAny(const AnyOf: array of Char): Integer;<br>function LastIndexOfAny(const AnyOf: array of Char; StartIndex: Integer): Integer;<br>function LastIndexOfAny(const AnyOf: array of Char; StartIndex: Integer; Count: Integer): Integer;<br>//--------------------------------------------------------------------------------<br>var<br> str: string;<br> n: Integer;<br>begin<br> str := 'A1 A2 A3 A4';<br><br> n := str.IndexOf('A'); // 0<br> n := str.LastIndexOf('A'); // 9<br> n := str.IndexOf('B'); // -1; 没找到<br><br> n := str.IndexOf('A', 1, str.Length - 1); // 3<br> n := str.LastIndexOf('A', str.Length - 1, str.Length - 1); // 9<br><br> n := str.IndexOfAny(['1', '2', '3', '4']); // 1<br> n := str.LastIndexOfAny(['1', '2', '3', '4']); // 10<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>是否包含:</strong><br>--------------------------------------------------------------------------------<br>function Contains(const Value: string): Boolean;<br>function StartsWith(const Value: string): Boolean;<br>function StartsWith(const Value: string; IgnoreCase: Boolean): Boolean;<br>function EndsWith(const Value: string): Boolean;<br>function EndsWith(const Value: string; IgnoreCase: Boolean): Boolean;<br>class function EndsText(const ASubText, AText: string): Boolean;<br>//--------------------------------------------------------------------------------<br>var<br> str: string;<br> b: Boolean;<br>begin<br> str := 'Delphi XE4';<br><br> b := str.Contains('XE'); // True<br> b := str.Contains('xe'); // False<br><br> b := str.StartsWith('delphi'); // False<br> b := str.StartsWith('delphi', True); // True<br><br> b := str.EndsWith('XE4'); // True<br><br> b := str.EndsText('xe4', str); // True<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>添加或解除引号:</strong><br>--------------------------------------------------------------------------------<br>function QuotedString: string;<br>function QuotedString(const QuoteChar: Char): string;<br>function DeQuotedString: string;<br>function DeQuotedString(const QuoteChar: Char): string;<br>//--------------------------------------------------------------------------------<br>var<br> str1, str2: string;<br>begin<br> str1 := 'Delphi';<br><br> str2 := str1.QuotedString; // 'Delphi'<br> str2 := str1.QuotedString('"'); // "Delphi"<br><br> str1 := '"Delphi"';<br> str2 := str1.DeQuotedString('"'); // Delphi<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>适宽处理:</strong><br>--------------------------------------------------------------------------------<br>function PadLeft(TotalWidth: Integer): string;<br>function PadLeft(TotalWidth: Integer; PaddingChar: Char): string;<br>function PadRight(TotalWidth: Integer): string;<br>function PadRight(TotalWidth: Integer; PaddingChar: Char): string;<br>//--------------------------------------------------------------------------------<br>var<br> str: string;<br>begin<br> str := '1';<br> str := str.PadLeft(4, '0'); // 0001<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>插入与删除:</strong><br>--------------------------------------------------------------------------------<br>function Insert(StartIndex: Integer; const Value: string): string;<br>function Remove(StartIndex: Integer): string;<br>function Remove(StartIndex: Integer; Count: Integer): string;<br>//--------------------------------------------------------------------------------<br>var<br> str1, str2: string;<br>begin<br> str1 := 'Delphi 4';<br> str2 := str1.Insert(7, 'XE'); // Delphi XE4<br><br> str1 := 'Delphi XE4';<br> str2 := str1.Remove(6); // Delphi<br> str2 := str1.Remove(7, 2); // Delphi 4<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>截取:</strong><br>--------------------------------------------------------------------------------<br>function Substring(StartIndex: Integer): string;<br>function Substring(StartIndex: Integer; Length: Integer): string;<br>//--------------------------------------------------------------------------------<br>var<br> str1, str2: string;<br>begin<br> str1 := 'Delphi XE4';<br> str2 := str1.Substring(7); // XE4<br> str2 := str1.Substring(7, 2); // XE<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>替换:</strong><br>--------------------------------------------------------------------------------<br>function Replace(OldChar: Char; NewChar: Char): string;<br>function Replace(OldChar: Char; NewChar: Char; ReplaceFlags: TReplaceFlags): string;<br>function Replace(const OldValue: string; const NewValue: string): string;<br>function Replace(const OldValue: string; const NewValue: string; ReplaceFlags: TReplaceFlags): string;<br>//--------------------------------------------------------------------------------<br>var<br> str1, str2: string;<br>begin<br> str1 := 'ABC ABC ABC';<br> str2 := str1.Replace('A', '*'); // *BC *BC *BC<br> str2 := str1.Replace('A', '*', ); // *BC ABC ABC<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>分割:</strong><br>--------------------------------------------------------------------------------<br>function Split(const Separator: array of Char): TArray;<br>function Split(const Separator: array of Char; Count: Integer): TArray;<br>function Split(const Separator: array of Char; Options: TStringSplitOptions): TArray;<br>function Split(const Separator: array of string; Options: TStringSplitOptions): TArray;<br>function Split(const Separator: array of Char; Count: Integer; Options: TStringSplitOptions): TArray;<br>function Split(const Separator: array of string; Count: Integer; Options: TStringSplitOptions): TArray;<br>//--------------------------------------------------------------------------------<br>var<br> str: string;<br> arr: TArray;<br>begin<br> str := 'A-1,B-2,,,C-3,D-4';<br><br> arr := str.Split([',']); // arr = A-1; Length(arr) = 6<br> arr := str.Split([','], TStringSplitOptions.ExcludeEmpty); // 忽略空项; Length(arr) = 4<br> arr := str.Split([','], 2); // 只提取前 2<br><br> arr := str.Split([',', '-'], ExcludeEmpty); //arr = A; Length(arr) = 8<br><br> arr := str.Split([',,,'], None); // 分隔符可以是一个字符串数组<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>连接:</strong><br>--------------------------------------------------------------------------------<br>class function Join(const Separator: string; const values: array of const): string;<br>class function Join(const Separator: string; const Values: array of string): string;<br>class function Join(const Separator: string; const Values: IEnumerator): string;<br>class function Join(const Separator: string; const Values: IEnumerable): string;<br>class function Join(const Separator: string; const value: array of string; StartIndex: Integer; Count: Integer): string;<br>//--------------------------------------------------------------------------------<br>var<br> S: string;<br> str: string;<br> strArr: TArray;<br>begin<br> str := 'A1,B2,C3,,,,D4,E5,F6,G7';<br> strArr := str.Split([','], ExcludeEmpty);<br><br> str := S.Join('-', strArr); // A1-B2-C3-D4-E5-F6-G7<br><br> str := S.Join('; ', ); // 1; 2; 3; 4; 5<br><br> str := S.Join(',', ['abc', 123, true]); // abc,123,True<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>类型转换:</strong><br>--------------------------------------------------------------------------------<br>function ToBoolean: Boolean;<br>function ToInteger: Integer;<br>function ToSingle: Single;<br>function ToDouble: Double;<br>function ToExtended: Extended;<br><br>class function ToBoolean(const S: string): Boolean;<br>class function ToInteger(const S: string): Integer;<br>class function ToSingle(const S: string): Single;<br>class function ToDouble(const S: string): Double;<br>class function ToExtended(const S: string): Extended;<br><br>class function Parse(const Value: Integer): string;<br>class function Parse(const Value: Int64): string;<br>class function Parse(const Value: Boolean): string;<br>class function Parse(const Value: Extended): string;<br>//--------------------------------------------------------------------------------<br>var<br> S: string;<br> str: string;<br> n: Integer;<br> b: Boolean;<br> f: Double;<br>begin<br> str := S.Parse(123);<br> n := str.ToInteger; // 123<br> b := str.ToBoolean; // True<br><br> str := S.Parse(True);<br> b := str.ToBoolean; // True<br> n := str.ToInteger; // -1<br><br> str := S.Parse(3.14159260000);<br> f := str.ToDouble; //3.1415926<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>定界符:</strong><br>--------------------------------------------------------------------------------<br>function IsDelimiter(const Delimiters: string; Index: Integer): Boolean;<br>function LastDelimiter(const Delims: string): Integer;<br>//--------------------------------------------------------------------------------<br>var<br> str: string;<br> b: Boolean;<br> n: Integer;<br>begin<br> str := 'http://del.cnblogs.com';<br><br> b := str.IsDelimiter(':', 4); // True<br> b := str.IsDelimiter('//', 5); // True<br><br> n := str.LastDelimiter('.'); // 18<br> n := str.IndexOf('.'); // 10<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>空字符串:</strong><br>--------------------------------------------------------------------------------<br>const Empty = '';<br>function IsEmpty: Boolean;<br>class function IsNullOrEmpty(const Value: string): Boolean;<br>class function IsNullOrWhiteSpace(const Value: string): Boolean;<br>//--------------------------------------------------------------------------------<br>var<br> S: string;<br> str: string;<br> b: Boolean;<br>begin<br> str := ' ';<br><br> b := str.IsEmpty; // False<br> b := S.IsNullOrWhiteSpace(str); // True<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>String 与 Char:</strong><br>--------------------------------------------------------------------------------<br>class function Create(C: Char; Count: Integer): string;<br>class function Create(const Value: array of Char; StartIndex: Integer; Length: Integer): string;<br>class function Create(const Value: array of Char): string;<br>property Chars: Char read GetChars;<br>property Length: Integer read GetLength;<br>function CountChar(const C: Char): Integer;<br>function ToCharArray: TArray;<br>function ToCharArray(StartIndex: Integer; Length: Integer): TArray;<br>procedure CopyTo(SourceIndex: Integer; var destination: array of Char; DestinationIndex: Integer; Count: Integer);<br>//--------------------------------------------------------------------------------<br>var<br> S: string;<br> str, str2: string;<br> charArr: TCharArray;<br> n: Integer;<br> c: Char;<br>begin<br> str := 'ABC';<br> n := str.Length; // 3<br> c := str.Chars; // A = str<br><br> str := S.Create('A', 7); // AAAAAAA<br><br> charArr := 'ABCDEFG'.ToCharArray;<br> str := s.Create(charArr); // ABCDEFG<br> str := S.Create(charArr, 1, 3); // BCD<br><br> charArr := '1234567890'.ToCharArray;<br> str := 'ABCDEFG';<br> str.CopyTo(1, charArr, 2, 3);<br> str := S.Create(charArr); // 12BCD67890<br>end;<br>--------------------------------------------------------------------------------<br><br><strong>其他:</strong><br>--------------------------------------------------------------------------------<br>function Equals(const Value: string): Boolean;<br>function GetHashCode: Integer;<br>class function Equals(const a: string; const b: string): Boolean;<br>class function Format(const Format: string; const args: array of const): string;<br>class function Copy(const Str: string): string;<br>//--------------------------------------------------------------------------------<br><br>// 用 Equals 不如直接用 = 号<br>// 用 Copy 不如直接用 :=<br>// 用 string.Format 不如直接用 Format()</div><br><br>
来源:https://www.cnblogs.com/qiufeng2014/p/16978764.html
頁:
[1]