首页 > 其他分享 >System.SysUtils.TStringHelper

System.SysUtils.TStringHelper

时间:2023-06-11 09:58:35浏览次数:54  
标签:function const string TStringHelper System SysUtils str Integer str1

大小写转换:


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;

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;

标签:function,const,string,TStringHelper,System,SysUtils,str,Integer,str1
From: https://www.cnblogs.com/del88/p/17472525.html

相关文章

  • System(系统类)
    System(系统类)运行环境:Java程序的运行平台1、System类提供了一些静态属性和方法,允许通过类名直接调用。2、System类提供了代表标准输入、标准输出、错误输出的类属性。3、System类提供了一些静态方法用于访问环境变量、系统属性的方法。知识点1:System(系统类)提供的属性研究System.o......
  • Database System Concepts——读书笔记 第二章 关系模型简介
    关系模型简介在关系模型中,术语relation用于指代table,而术语tuple用于指代row。类似地,术语attribute(属性)指的是表中的一column(列)。我们必须区分数据库模式和数据库实例,前者是数据库的逻辑设计,后者是给定时刻数据库中数据的快照。关系的模式指的是它的逻辑设计,而关系的实例指的......
  • Database System Concepts——读书笔记 第一章 介绍
    数据库系统概念——第一章数据库管理系统(DBMS)由相互关联的数据集合和访问这些数据的程序集合组成。数据库相对于文件系统,更规范化,提供条件查询能力,避免冗余数据。类似操作系统于底层硬件,提供抽象能力,易用性。physicallevel->logicallevel->viewlevelinstance和schem......
  • Database System Concepts——读书笔记 第三、四、五章 SQL简介
    SQL简介关系代数运算和SQL运算之间有着密切的联系。一个关键的区别是,与关系代数不同,SQL允许重复与select子句不同,union联合操作会自动消除重复项.如果我们想保留所有的副本,我们就必须用“unionall”代替“union.intersectall,exceptall您可以验证,如果r.A为null,则“1<r.A”......
  • 关于The JSON value could not be converted to System.DateTime的解决方案
    如下json格式提交到后台后报:TheJSONvaluecouldnotbeconvertedtoSystem.DateTime.Path:$.beginTime|LineNumber:3|BytePositionInLine:33.{"beginTime":"2023-06-08T08:00:00"}造成这个错误的原因为程序无法正常解析该json,主要是为了提升执行效率;Sys......
  • windows 10 wsl 环境 docker 无法正常启动 -The system cannot find the file specif
    错误信息:errorduringconnect:inthedefaultdaemonconfigurationonWindows,thedockerclientmustberunwithelevatedprivilegestoconnect:Get"http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/json":open//./pipe/docker_engine:Thesy......
  • 检测到包降级: System.Diagnostics.Debug 从 4.3.0 降级到 4.0.11。直接从项目引用包
    .net 项目在发版的时候报包的版本不一致严重性代码说明项目文件行禁止显示状态错误错误形式的警告:检测到包降级:System.Diagnostics.Debug从4.3.0降级到4.0.11。直接从项目引用包以选择不同版本。ProjectName->Microsoft.AspNetCore.Mvc.Core2.2.5->Micros......
  • org.springframework.data.redis.RedisSystemException: Redis exception; nested exc
    springBoot+redis.程序隔一段时间会莫名其妙的报Redis的错误.报错如下:org.springframework.data.redis.RedisSystemException:Redisexception;nestedexceptionisio.lettuce.core.RedisException:java.io.IOException:Connectionresetbypeer百度得知说:是因为re......
  • 使用 ADB 命令为 Windows Subsystem Android(WSA)配置代理
    注意!以下命令需在命令提示符中执行,不要使用PowerShell,会有字符错误!设置代理:adbconnect127.0.0.1:58526&&adbshell"settingsputglobalhttp_proxy`iproutelistmatch0tableallscopeglobal|cut-F3`:7890"中间一大段会自动识别宿主机ip注意修改7890端口......
  • 文件系统考古2:1984 - BSD Fast Filing System
    今天继续与大家分享系列文章《50yearsinfilesystems》,由KRISTIANKÖHNTOPP撰写。我们将进入文件系统的第二个十年,即1984年,计算机由微型计算机发展到了桌面和机柜工作站,BSDFastFilingSystem登场。回看第一篇:1974-UnixV7FileSystem早期的Unix文件系统已经表现得......