测试 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< ...
随机推荐
- 1096: [ZJOI2007]仓库建设
1096: [ZJOI2007]仓库建设 思路 斜率优化. 代码 #include<cstdio> #include<iostream> using namespace std ...
- 9path 导致的一场冤假错案
今天做对话框开发,遇到一个问题,就是弹出来的对话框太丑了.如图: 大家都是warp_content, 前面几个就是真的wrap_coment了.只有最后一个还可以看.后来自己找代码,写的都一样,就去问 ...
- 斐波那契数列(Fibonacci) iOS
斐波那契数列Fibonacci 斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2 ...
- python学习笔记十:异常
一.语法 #!/usr/bin/python filename='hello' #try except finally demo try: open('abc.txt') print hello ex ...
- IOS测试,打不开要测试的APP怎么办?设置信任
步骤:设置-->通用-->设备管理-->企业级应用-->信任 具体教程:http://jingyan.baidu.com/article/ab69b27085ab002ca71 ...
- [转载]python 变量命名规范
原文地址:python 变量命名规范作者:loveflying python源码和其他一些书籍,命名各种个性,没有一个比较统一的命名规范.于是自己总结了一些,可供参考. 模块名: 小写字母,单词之间用 ...
- 指定user镜像安装的磁盘
ironic node-update <node uuid> add properties/root_device='{"name":"/dev/sdb&qu ...
- TensorFlow dataset API 使用
# TensorFlow dataset API 使用 由于本人感兴趣的是自然语言处理,所以下面有关dataset API 的使用偏向于变长数据的处理. 1. 从迭代器中引入数据 import num ...
- PCA学习笔记
主成分分析(Principal Component Analysis,简称PCA)是最常用过的一种降维方法 在引入PCA之前先提到了如何使用一个超平面对所有的样本进行恰当的表达? 即若存在这样的超平面 ...
- PEAR DB 初学笔记
1.数据查询 i. DB_common::getAll() DB_FETCHMODE_ORDERED . DB_FETCHMODE_ASSOC . DB_FETCHMODE_OBJECT ii. DB ...