测试 System.SysUtils.TStringHelper
来自:http://www.cnblogs.com/del/archive/2013/06/14/3135002.html
-------------------------------------------------------------------------------
大小写转换:
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, , str2, , ); // 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', , str.Length - ); // 3
n := str.LastIndexOf('A', str.Length - , str.Length - ); // 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(, '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(, 'XE'); // Delphi XE4 str1 := 'Delphi XE4';
str2 := str1.Remove(); // Delphi
str2 := str1.Remove(, ); // 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(); // XE4
str2 := str1.Substring(, ); // 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<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 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 str := S.Join(',', ['abc', , 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();
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(':', ); // True
b := str.IsDelimiter('//', ); // 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[]; // A = str[1] str := S.Create('A', ); // AAAAAAA charArr := 'ABCDEFG'.ToCharArray;
str := s.Create(charArr); // ABCDEFG
str := S.Create(charArr, , ); // BCD charArr := '1234567890'.ToCharArray;
str := 'ABCDEFG';
str.CopyTo(, charArr, , );
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() // 总之, 还是有用处的多!
测试 System.SysUtils.TStringHelper的更多相关文章
- System.SysUtils.TMarshaller 与 System.TMarshal
转自:http://www.cnblogs.com/del/archive/2013/06/10/3130974.html TMarshaller(结构) 基于 TMarshal(是有一大堆的 cla ...
- Delphi TStringHelper用法详解
Delphi TStringHelper用法详解 (2013-08-27 22:45:42) 转载▼ 标签: delphi_xe5 it 分类: Delphi Delphi XE4的TStringHe ...
- Delphi XE4 TStringHelper用法详解
原文地址:Delphi XE4 TStringHelper用法详解作者:天下为公 Delphi XE4的TStringHelper,对操作字符串进一步带来更多的方法,估计XE5还能继续用到. Syst ...
- 写了一个字符串的二维表: TSta
STA 单元 (用到 System.SysUtils.TStringHelper): --------------------------------------------------------- ...
- System.IO在不存在的路径下创建文件夹和文件的测试
本文测试System.IO命名空间下的类,在不存在的路径下创建文件夹和文件的效果: 首先测试创建文件夹: System.IO.Directory.CreateDirectory(@"C:\A ...
- Android实例-为程序创建快捷方式(未测试)
结果: 1.因为只有小米手机,没有三星手机,没法测试.如果哪位神测试过的话,记得M我哦,谢了. 实例代码: unit Unit1; interface uses System.SysUtils, S ...
- delphi 多线程之System.TMonitor
三天不写代码就手生! 把测试代码记录下来. unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, S ...
- Xe7 System.Json解析数据格式
一.Demo一 解析嵌套数组 Json数据 {"code":1,"msg":"","data":{"Grade ...
- delphi xe6 JSON 测试
System.JSON ISuperJSOn mORMETJSON QJSON 测试 我在测试时发现系统自带的JSON 占用内存大一但多了就会出现内存泄漏的问题 我用的Flst< ...
随机推荐
- 2 实现第一个Django网站 博客
-1.理解上下文 render()渲染 request url传来的reuqest x.html 制定返回的模板名称 context 上下文 数据库中 替换数据 0.大框架 1.创建模板 (1 ...
- eclipse、myeclipse 反编译插件 轻松查看源代码
java反编译插件:Eclipse Class Decompiler,能够很方便的使用本插件查看类库源码,以及采用本插件进行Debug调试. eclipse中安装Eclipse Class Decom ...
- DOS程序员手册(一)
当今MS-Windows横扫大江南北,让我们这就来研究一下它的祖宗——MS-DOS! 这本书很难得,希望读者好好学习! DOS程序员手册(一) DOS教程 (以下内容全部为原作者的阐述,照样保留) 这 ...
- nginx清除反向代理缓存
nginx重启无法清除反向代理的缓存,可以清空安装目录下的proxy_cache文件夹里的内容来清除.
- BInder浅析
Binder是什么 Binder是运行在Android内核态用于进程间通信(IPC)的驱动,采用C/S架构,由三项基本组件组成:Binder服务端,Binder驱动,应用程序客户端. 为什么要用Bin ...
- 云计算之路-阿里云上:SLB引起的https访问速度慢问题
自从我们在阿里云SLB上部署了https之后(详见在SLB上部署https遇到的问题及解决方法),陆续有园友向我们反馈登录时速度很慢(登录时是通过https访问),有些园友甚至无法访问登录页面. 而我 ...
- Java中Set的contains()方法——hashCode与equals方法的约定及重写原则
转自:http://blog.csdn.net/renfufei/article/details/14163329 翻译人员: 铁锚 翻译时间: 2013年11月5日 原文链接: Java hashC ...
- appium-手势密码实现-automationName 是automator2
上一篇博客已经说了 appium-手势密码实现-automationName 是Appium的情况 下面就说一下automator2的情况: 手势密码的moveTo方法的参数进行了改变. 参数是相对于 ...
- Ubuntu15.04 python升级到python-3.6.x
简略记录步骤,容后补充: sudo add-apt-repository ppa:jonathonf/python-3.6 sudo apt-get update sudo apt-get i ...
- TensorFlow dataset API 使用
# TensorFlow dataset API 使用 由于本人感兴趣的是自然语言处理,所以下面有关dataset API 的使用偏向于变长数据的处理. 1. 从迭代器中引入数据 import num ...