测试 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< ...
随机推荐
- mysql安装与基本管理,mysql密码破解
一.MySQL介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是 ...
- dex2oat 加载多次
我是一个做插件开发的人员,插件就是动态加载dex文件. 然后4.4以后,Android dalvik虚拟机,和ART. art运行的必须要把dex转换为oat elf文件. 然后,这个dex2oat需 ...
- Leetcode 659.分割数组为连续子序列
分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [1,2,3 ...
- [muku][1 初始restful api] chorme安装jsonview 插件
https://github.com/gildas-lormeau/JSONView-for-Chrome https://www.cnblogs.com/androidstudy/p
- SpringBoot Rabbitmq发送消息
官方文档:https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#boot-features-amqp ...
- 团队冲刺Alpha(十)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- hdu1877进制转换
#include <stdio.h> int m; void Ck(int n) { if(n>=m) Ck(n/m); printf("%d",n%m); } ...
- Linux下nginx支持.htaccess文件实现伪静态的方法!
在Google上搜索的资料很多人都说nginx目前不支持.htaccess文件,我按照nginx的规则试验了一下,结果发现nginx是完全支持.htaccess文件的! 方法如下: 1. 在需要使用. ...
- asp.net 条码 一维条码 生成, 一共有32中格式类型
想用asp.net 实现条码功能,网上找了代码都不全. 终于找到了一个非常全的DLL 是winForm的,原以为不能用在WEB 上结果 可以使用. 首先,引用 DLL 不必说了,下面是 使用设置.还有 ...
- 【bzoj2959】长跑 LCT+并查集
题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前.为了 ...