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;
插入(startIndex从0开始,严重注意,insert是直接把原来的字符串销毁,生成一个新的字符串):
function Insert(StartIndex: Integer; const Value: string): string;
procedure TForm3.btn2Click(Sender: TObject);
var
s1, s2: string;
begin
s1 := '123456789';
s2 := s1.Insert(0, '0'); //startIndex从0开始
mmo1.Lines.Add(s1); // 0123456789,插入是直接把原来的字符串销毁掉,直接生成新的字符串
mmo1.Lines.Add(s2); //0123456789,
end;
删除(startIndex是从0开始,严重注意,remove是生成【另】一个新的字符串,原来的字符串不变):
function Remove(StartIndex: Integer): string;
function Remove(StartIndex: Integer; Count: Integer): string;
procedure TForm3.btn1Click(Sender: TObject);
var
s1, s2: string;
begin
s1 := '123456789';
s2 := s1.Remove(1, 1); //Remove 会形成新的字符串,原来的s1不变
mmo1.Lines.Add(s1.Remove(0, 1)); // 23456789,Remove是下标从0开始
mmo1.Lines.Add(s1.Remove(1, 1));
mmo1.Lines.Add(s1); //'123456789',可见s1并没有发生改变
mmo1.Lines.Add(s2);
end;
截取(startIndex是从0开始):
function Substring(StartIndex: Integer): string;
function Substring(StartIndex: Integer; Length: Integer): string;
procedure TForm3.btn1Click(Sender: TObject);
var
s: string;
begin
s := '123456';
mmo1.Lines.Add(s[1]); //1,下标从1开始
mmo1.Lines.Add(s.Substring(1, 1)); //2,下标从0开始
end;
subString的下标是从0开始的严重留意。。。
替换:
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<string>;
function Split(const Separator: array of Char; Count: Integer): TArray<string>;
function Split(const Separator: array of Char; Options: TStringSplitOptions): TArray<string>;
function Split(const Separator: array of string; Options: TStringSplitOptions): TArray<string>;
function Split(const Separator: array of Char; Count: Integer; Options: TStringSplitOptions): TArray<string>;
function Split(const Separator: array of string; Count: Integer; Options: TStringSplitOptions): TArray<string>;
//--------------------------------------------------------------------------------
var
str: string;
arr: TArray<string>;
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>): string;
class function Join(const Separator: string; const Values: IEnumerable<string>): string;
class function Join(const Separator: string; const value: array of string; StartIndex: Integer; Count: Integer): string;
//--------------------------------------------------------------------------------
var
S: string;
str: string;
strArr: TArray<string>;
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<Char>;
function ToCharArray(StartIndex: Integer; Length: Integer): TArray<Char>;
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;
System.SysUtils.TStringHelper 详解,注意事项的更多相关文章
- orakill和ALTER SYSTEM KILL SESSION详解
--orakill和ALTER SYSTEM KILL SESSION详解[转]-----------------------------------------2013/11/05 一个用户进程偶尔 ...
- 谈谈分布式事务之三: System.Transactions事务详解[下篇]
在前面一篇给出的Transaction的定义中,信息的读者应该看到了一个叫做DepedentClone的方法.该方法对用于创建基于现有Transaction对 象的“依赖事务(DependentTra ...
- 谈谈分布式事务之三: System.Transactions事务详解[上篇]
在.NET 1.x中,我们基本是通过ADO.NET实现对不同数据库访问的事务..NET 2.0为了带来了全新的事务编程模式,由于所有事务组件或者类型均定义在System.Transactions程序集 ...
- System.out.println()详解 和 HttpServletRequest 和 XMLHttpRequest
System是一个类,位于java.lang这个包里面.out是这个System类的一个PrintStream类型静态属性.println()是这个静态属性out所属类PrintStream的方法. ...
- System.AppDomain类详解(二)
进程是存在独立的内存和资源的,但是AppDomain仅仅是逻辑上的一种抽象.一个process可以存在多个AppDomain.各个AppDomain之间的数据时相互独立的.一个线程可以穿梭多个AppD ...
- System.AppDomain类详解(一)
AppDomain是CLR(Common Language Runtime:公共语言运行库),它可以加载Assembly.创建对象以及执行程序. AppDomain是CLR实现代码隔离的基本机制. 每 ...
- C# System.Threading.Timer 详解及示例
前言 定时器功能在日常开发中也是比较常用的,在 .Net 中实际上总共有五种定时器,分别是:System.Timers.Timer.System.Threading.Timer.System.Wind ...
- System.arraycopy方法详解
- 测试 System.SysUtils.TStringHelper
来自:http://www.cnblogs.com/del/archive/2013/06/14/3135002.html -------------------------------------- ...
- Delphi TStringHelper用法详解
Delphi TStringHelper用法详解 (2013-08-27 22:45:42) 转载▼ 标签: delphi_xe5 it 分类: Delphi Delphi XE4的TStringHe ...
随机推荐
- 【转载】内存基本概念-watermark&lowmem_reserve
概述 当系统内存短缺的情况下仍去申请内存,可能会触发系统对内存的回收,那什么时候应该进行回收,回收到什么标准又可以停止回收,参考依据是什么?即本文将介绍的watermark(内存水位线),当检查wat ...
- python 基础 | 实现微秒级计时
搬运一个计时代码: import datetime s = datetime.datetime.now() # 开始 # do something e = datetime.datetime.now( ...
- 05-逻辑仿真工具VCS-执行过程
Verilog Simulation Event Queue 主要了解VCS是如何处理交给它的代码的 Verilog的仿真事件队列,介绍VCS如何处理交给它的代码.VCS是Synopsys公司的,支持 ...
- C++中不支持strdup(),使用_strdup()
1.问题 C4996 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conf ...
- CSS : div 高度为0的三种情况
1, css 样式没正确绑定 ( 也就是没有设置高度 ) 2, 子元素 浮动 ( float ) 3, 子元素 绝对定位 ( position : absolute )
- WebApi允许跨域
services.AddCors(options => { options.AddPolicy("abc", builder => { //App:CorsOrigin ...
- Go-稀疏数组
package main import "fmt" // 稀疏数组 // 1. 二维数组 // 2. 存在大量相同相同数据和少量不同数据 // 思维: 将大量相同数据转化为: (数 ...
- [转帖]centos7离线安装postgresql13
https://www.cnblogs.com/summer-88/p/15341918.html 在一台可以联网的centos上安装postgresql源 yum install -y https: ...
- [转帖]jmeter正则表达式应用-01篇
如图所示 1.先新建一个login的http请求,然后再login的请求下新增一个正则表达式提取器,增加一个查看结果树查看结果 假如后端接口返回的数据为"{'msg': 'login suc ...
- [转帖]Ceph优化系列(四):RocksDB 使用 ARM 64 位 CRC32C 硬件优化指令
一.前言 CRC32(A cyclic redundancy check 32)应用于校验,为了保证数据的正确性,采用的一种检错手段. CRC32C (CRC32 Castagnoli) 与 CRC ...