Delphi XE4的TStringHelper,对操作字符串进一步带来更多的方法,估计XE5还能继续用到。

System.SysUtils.TStringHelper

大小写转换:
--------------------------------------------------------------------------------
 
function ToLower: string;
function ToLower(LocaleID: TLocaleID): string;
function ToLowerInvariant: string;
function ToUpper: string;
function ToUpper(LocaleID: TLocaleID): string;
function ToUpperInvariant: string;

class function LowerCase(const S: string): string;
class function LowerCase(const S: string; LocaleOptions: TLocaleOptions): string;
class function UpperCase(const S: string): string;
class function UpperCase(const S: string; LocaleOptions: TLocaleOptions): string;
//--------------------------------------------------------------------------------
var
  str: string;
begin
  str := 'Delphi';
  str := str.ToLower; // delphi
  str := str.ToUpper; // DELPHI
end;
--------------------------------------------------------------------------------

清除两边空格或指定字符:
--------------------------------------------------------------------------------
 
function Trim: string;
function TrimLeft: string;
function TrimRight: string;
function Trim(const TrimChars: array of Char): string;
function TrimLeft(const TrimChars: array of Char): string;
function TrimRight(const TrimChars: array of Char): string;
//--------------------------------------------------------------------------------
var
  str1, str2: string;
begin
  str1 := '   Delphi 10000   ';

str2 := str1.TrimLeft;  // 'Delphi 10000   '
  str2 := str1.TrimRight; // '   Delphi 10000'
  str2 := str1.Trim;      // 'Delphi 10000'

str2 := str1.Trim([' ', '0']); // 'Delphi 1'
end;
--------------------------------------------------------------------------------

字符串对比:
--------------------------------------------------------------------------------
 
function CompareTo(const strB: string): Integer;

class function Compare(const StrA: string; const StrB: string): Integer;
class function CompareText(const StrA: string; const StrB: string): Integer;
class function Compare(const StrA: string; const StrB: string; LocaleID: TLocaleID): Integer;
class function Compare(const StrA: string; const StrB: string; IgnoreCase: Boolean): Integer;
class function Compare(const StrA: string; const StrB: string; IgnoreCase: Boolean; LocaleID: TLocaleID): Integer;
class function Compare(const StrA: string; IndexA: Integer; const StrB: string; IndexB: Integer; Length: Integer): Integer;
class function Compare(const StrA: string; IndexA: Integer; const StrB: string; IndexB: Integer; Length: Integer; LocaleID: TLocaleID): Integer;
class function Compare(const StrA: string; IndexA: Integer; const StrB: string; IndexB: Integer; Length: Integer; IgnoreCase: Boolean): Integer;
class function Compare(const StrA: string; IndexA: Integer; const StrB: string; IndexB: Integer; Length: Integer; IgnoreCase: Boolean; LocaleID: TLocaleID): Integer;
class function CompareOrdinal(const StrA: string; const StrB: string): Integer;
class function CompareOrdinal(const StrA: string; IndexA: Integer; const StrB: string; IndexB: Integer; Length: Integer): Integer;
//--------------------------------------------------------------------------------
var
  str1, str2: string;
  n: Integer;
begin
  str1 := 'ABC 123';
  str2 := 'abc 123';

n := str1.CompareTo(str2);              // -32

n := str1.Compare(str1, str2);          // 1
  n := str1.CompareText(str1, str2);      // 0; 相同

n := str1.Compare(str1, str2, True);    // 0; 不区分大小写
  n := str1.CompareOrdinal(str1, str2);   // -32

n := str1.Compare(str1, 4, str2, 4, 3); // 0; 只对比后三位
end;
--------------------------------------------------------------------------------

搜索字符串:
--------------------------------------------------------------------------------
 
function IndexOf(value: Char): Integer;
function IndexOf(const Value: string): Integer;
function IndexOf(Value: Char; StartIndex: Integer): Integer;
function IndexOf(const Value: string; StartIndex: Integer): Integer;
function IndexOf(Value: Char; StartIndex: Integer; Count: Integer): Integer;
function IndexOf(const Value: string; StartIndex: Integer; Count: Integer): Integer;
function IndexOfAny(const AnyOf: array of Char): Integer;
function IndexOfAny(const AnyOf: array of Char; StartIndex: Integer): Integer;
function IndexOfAny(const AnyOf: array of Char; StartIndex: Integer; Count: Integer): Integer;

function LastIndexOf(Value: Char): Integer;
function LastIndexOf(const Value: string): Integer;
function LastIndexOf(Value: Char; StartIndex: Integer): Integer;
function LastIndexOf(const Value: string; StartIndex: Integer): Integer;
function LastIndexOf(Value: Char; StartIndex: Integer; Count: Integer): Integer;
function LastIndexOf(const Value: string; StartIndex: Integer; Count: Integer): Integer;
function LastIndexOfAny(const AnyOf: array of Char): Integer;
function LastIndexOfAny(const AnyOf: array of Char; StartIndex: Integer): Integer;
function LastIndexOfAny(const AnyOf: array of Char; StartIndex: Integer; Count: Integer): Integer;
//--------------------------------------------------------------------------------
var
  str: string;
  n: Integer;
begin
  str := 'A1 A2 A3 A4';

n := str.IndexOf('A');     // 0
  n := str.LastIndexOf('A'); // 9
  n := str.IndexOf('B');     // -1; 没找到

n := str.IndexOf('A', 1, str.Length - 1);                  // 3
  n := str.LastIndexOf('A', str.Length - 1, str.Length - 1); // 9

n := str.IndexOfAny(['1', '2', '3', '4']);     // 1
  n := str.LastIndexOfAny(['1', '2', '3', '4']); // 10
end;
--------------------------------------------------------------------------------

是否包含:
--------------------------------------------------------------------------------
 
function Contains(const Value: string): Boolean;

function StartsWith(const Value: string): Boolean;
function StartsWith(const Value: string; IgnoreCase: Boolean): Boolean;

function EndsWith(const Value: string): Boolean;
function EndsWith(const Value: string; IgnoreCase: Boolean): Boolean;

class function EndsText(const ASubText, AText: string): Boolean;
//--------------------------------------------------------------------------------
var
  str: string;
  b: Boolean;
begin
  str := 'Delphi XE4';

b := str.Contains('XE'); // True
  b := str.Contains('xe'); // False

b := str.StartsWith('delphi');       // False
  b := str.StartsWith('delphi', True); // True

b := str.EndsWith('XE4');            // True

b := str.EndsText('xe4', str);       // True
end;
--------------------------------------------------------------------------------

添加或解除引号:
--------------------------------------------------------------------------------
 
function QuotedString: string;
function QuotedString(const QuoteChar: Char): string;

function DeQuotedString: string;
function DeQuotedString(const QuoteChar: Char): string;
//--------------------------------------------------------------------------------
var
  str1, str2: string;
begin
  str1 := 'Delphi';

str2 := str1.QuotedString;        // 'Delphi'
  str2 := str1.QuotedString('"');   // "Delphi"

str1 := '"Delphi"';
  str2 := str1.DeQuotedString('"'); // Delphi
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()

// 总之, 还是有用处的多!

Delphi XE4 TStringHelper用法详解的更多相关文章

  1. Delphi TStringHelper用法详解

    Delphi TStringHelper用法详解 (2013-08-27 22:45:42) 转载▼ 标签: delphi_xe5 it 分类: Delphi Delphi XE4的TStringHe ...

  2. Delphi常用关键字用法详解

    本文详细介绍了Delphi中常用的各个关键字名称及用法,供大家在编程过程中借鉴参考之用.详情如下: absolute: ? 1 2 3 4 5 6 7 8 9 10 //它使得你能够创建一个新变量, ...

  3. 教程-Delphi中Spcomm使用属性及用法详解

    Delphi中Spcomm使用属性及用法详解 Delphi是一种具有 功能强大.简便易用和代码执行速度快等优点的可视化快速应用开发工具,它在构架企业信息系统方面发挥着越来越重要的作用,许多程序员愿意选 ...

  4. delphi中Application.MessageBox函数用法详解

    delphi中Application.MessageBox函数用法详解 Application.MessageBox是TApplication的成员函数,声明如下:functionTApplicati ...

  5. delphi TStringList 用法详解

    转自: http://blog.163.com/you888@188/blog/static/67239619201472365642633/ delphi TStringList 用法详解 2014 ...

  6. Delphi Format函数功能及用法详解

    DELPHI中Format函数功能及用法详解 DELPHI中Format函数功能及用法详解function Format(const Format: string; const Args: array ...

  7. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  8. @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...

  9. linux管道命令grep命令参数及用法详解---附使用案例|grep

    功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...

随机推荐

  1. vue组件化编程

    vue文件包含3个部分 <template> <div></div> </template> <script> export default ...

  2. Linux学习-rsyslog.service :记录登录文件的服务

    rsyslog.service 的配置文件:/etc/rsyslog.conf 我们现在知道 rsyslogd 可以负责主机产生的各个信息的登录,而这些信息本身是有『严重等级』之分的, 而且, 这些资 ...

  3. JSON解析工具——fastjson的简单使用

    从官方文档入手: 常见问题与快速上手:https://github.com/alibaba/fastjson/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98 各种使 ...

  4. 《C++ Primer》第II部分:C++标准库

    <C++ Primer>第II部分:C++标准库 前言 把<C++ Primer>读薄系列笔记.本篇为第II部分C++标准库,包含全书第8-12章重难点: IO库 顺序容器 范 ...

  5. python 解决url编译

    from urllib import parse s = parse.unquote("%7B%22name%22%3A%22joe%22%2C%22age%22%3A%2223%22%7D ...

  6. VIM第七版

    ZZ:退出并保存 e!:退回到上次保存时的样子 cw:修改单词(自动进入插入模式) cc:修改一整行的内容 cs:修改一个词(自动进入插入模式) .:可以重复上一个命令 J:将下一行内容合并到本行末尾 ...

  7. lesson 22 by heart

    lesson 22 by heart on end = continuously 连续不断地 know/learn sth by heart 记忆sth falter: speak hesitantl ...

  8. hihocoder刷题 扫雷游戏

    题目1 : 扫雷游戏 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N × N的方格矩阵,其中每个格子或者是'*',表示该位置有一个地雷:或者是'.',表示该位 ...

  9. CSP201503-2:数字排序

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  10. 技本功丨知否知否,Redux源码竟如此意味深长(上集)

    夫 子 说 元月二号欠下袋鼠云技术公号一篇关于Redux源码解读的文章,转眼月底,期间常被“债主”上门催债.由于年底项目工期比较紧,于是债务就这样被利滚利.但是好在这段时间有点闲暇,于是赶紧把这篇文章 ...