软件的基本是要处理好”算法“及其基础(三)delphi 10.4.1字符串工具单元及其函数

(积累跟新中...)

一、字符串描述与定义

//:关于字符串的定义:
  _RawByteStr = RawByteString;
  {$IFDEF NEXTGEN}
    UTF8String = type _AnsiString(65001);
    RawByteString = type _AnsiString($ffff);  //:$ffff=  65535
    {$NODEFINE UTF8String}
    {$NODEFINE RawByteString}
  {$ELSEIF Defined(LINUX64) or Defined(OSX64) or Defined(ANDROID)}
    UTF8String = type AnsiString(65001);
    RawByteString = type AnsiString($ffff);  //:$ffff=  65535
    {$NODEFINE UTF8String}
    {$NODEFINE RawByteString}
  {$ELSE}
    UTF8String = type AnsiString(65001);
    RawByteString = type AnsiString($ffff);  //:$ffff=  65535
  {$ENDIF}
    PUTF8String = ^UTF8String;
    PRawByteString = ^RawByteString;
  {$IF Defined(NEXTGEN) or Defined(LINUX64) or Defined(OSX64) or Defined(ANDROID)}
    {$NODEFINE PUTF8String}
    {$NODEFINE PRawByteString}
  {$ENDIF NEXTGEN or LINUX64 or OSX64 or ANDROID}

二、你可能不常用的字符串函数:delphi.System.StrUtils.pas字符串工具单元中的函数

unit System.StrUtils;

interface

{$IFDEF CPUX86}

{$DEFINE X86ASM}

{$ELSE !CPUX86}

{$DEFINE PUREPASCAL}

{$ENDIF !CPUX86}

{$HPPEMIT LEGACYHPP}

uses

System.Types, System.SysUtils;

{$WARN WIDECHAR_REDUCED OFF}

{如果两个字符串相似(使用Soundex算法或相似的东西),则AnsiResemblesText返回true}

function ResemblesText(const AText, AOther: string): Boolean; overload; function AnsiResemblesText(const AText, AOther: string): Boolean; overload;

{如果在给定文本中找到子文本(不区分大小写),则AnsiContainsText返回true}

function ContainsText(const AText, ASubText: string): Boolean; inline; overload; function AnsiContainsText(const AText, ASubText: string): Boolean; overload;

{如果给定文本的开头或结尾部分与子文本匹配,且不区分大小写,则AnsiStartsText和AnsiEndText返回true}

function StartsText(const ASubText, AText: string): Boolean; inline; overload; function AnsiStartsText(const ASubText, AText: string): Boolean; overload;

function EndsText(const ASubText, AText: string): Boolean; inline; overload; function AnsiEndsText(const ASubText, AText: string): Boolean; inline; overload;

{AnsiReplaceText会在不区分大小写的情况下用另一个子字符串替换所有出现的子字符串(不支持递归子字符串替换)}

function ReplaceText(const AText, AFromText, AToText: string): string; inline; overload; function AnsiReplaceText(const AText, AFromText, AToText: string): string; overload; { AnsiMatchText & AnsiIndexText provide case like function for dealing with strings }

function MatchText(const AText: string; const AValues: array of string): Boolean; overload;

function AnsiMatchText(const AText: string; const AValues: array of string): Boolean; overload;

function IndexText(const AText: string; const AValues: array of string): Integer; overload;

function AnsiIndexText(const AText: string; const AValues: array of string): Integer; overload;

{这些功能与上述功能类似,但区分大小写}

function ContainsStr(const AText, ASubText: string): Boolean; inline; overload;

function AnsiContainsStr(const AText, ASubText: string): Boolean; overload;

function StartsStr(const ASubText, AText: string): Boolean; inline; overload;

function AnsiStartsStr(const ASubText, AText: string): Boolean; overload;

function EndsStr(const ASubText, AText: string): Boolean; inline; overload;

function AnsiEndsStr(const ASubText, AText: string): Boolean; overload;

function ReplaceStr(const AText, AFromText, AToText: string): string; inline; overload;

function AnsiReplaceStr(const AText, AFromText, AToText: string): string; overload;

function MatchStr(const AText: string; const AValues: array of string): Boolean; overload;

function AnsiMatchStr(const AText: string; const AValues: array of string): Boolean; overload;

function IndexStr(const AText: string; const AValues: array of string): Integer; overload;

function AnsiIndexStr(const AText: string; const AValues: array of string): Integer; overload;

{DupeString将返回给定字符串的N个副本}

function DupeString(const AText: string; ACount: Integer): string; overload;

{ReverseString简单地反转给定的字符串}

function ReverseString(const AText: string): string; overload;

function AnsiReverseString(const AText: string): string; overload;

{StuffString用另一个替换字符串的一部分}

function StuffString(const AText: string; AStart, ALength: Cardinal;

const ASubText: string): string; overload;

{RandomFrom将随机返回给定的字符串之一}

function RandomFrom(const AValues: array of string): string; overload;

{如果传入的值为true,IfIfn将返回真字符串,否则将返回假字符串}

function IfThen(AValue: Boolean; const ATrue: string; AFalse: string = ''): string; overload; inline;

{SplitString将字符串拆分为由指定的分隔符分隔的不同部分}

function SplitString(const S, Delimiters: string): TStringDynArray;

{类似基本功能/左,右,中}

{$IFNDEF NEXTGEN}

function LeftStr(const AText: AnsiString; const ACount: Integer): AnsiString; overload; deprecated 'Moved to the AnsiStrings unit';

function RightStr(const AText: AnsiString; const ACount: Integer): AnsiString; overload; deprecated 'Moved to the AnsiStrings unit';

function MidStr(const AText: AnsiString; const AStart, ACount: Integer): AnsiString; overload; deprecated 'Moved to the AnsiStrings unit';

{类似基本功能/ LeftB,RightB,MidB这些功能不在乎语言环境信息。}

function LeftBStr(const AText: AnsiString; const AByteCount: Integer): AnsiString; deprecated 'Moved to the AnsiStrings unit';

function RightBStr(const AText: AnsiString; const AByteCount: Integer): AnsiString; deprecated 'Moved to the AnsiStrings unit';

function MidBStr(const AText: AnsiString; const AByteStart, AByteCount: Integer): AnsiString; deprecated 'Moved to the AnsiStrings unit';

{$ENDIF !NEXTGEN}

function LeftStr(const AText: string; const ACount: Integer): string; overload;

function RightStr(const AText: string; const ACount: Integer): string; overload;

function MidStr(const AText: string; const AStart, ACount: Integer): string; overload;

{类基本函数/ Delphi样式函数名称}

function AnsiLeftStr(const AText: string; const ACount: Integer): string; overload;

function AnsiRightStr(const AText: string; const ACount: Integer): string; overload;

function AnsiMidStr(const AText: string; const AStart, ACount: Integer): string; overload;

type

TStringSeachOption = (soDown, soMatchCase, soWholeWord);

TStringSearchOptions = set of TStringSeachOption;

{SearchBuf是用于任意文本缓冲区的搜索例程。如果找到匹配项,该函数将返回一个指向缓冲区中匹配字符串开头的指针。如果找不到匹配项,则该函数返回nil。如果指定了soDown,则执行正向搜索,否则功能将向后搜索文本。使用SelStart和SelLength跳过“选定的”文本;这将导致搜索在指定文本之前(soDown)之前或之后开始。}

const

{默认字定界符是除核心字母数字外的任何字符。}

WordDelimiters: set of Byte = [0..255] - [Ord('a')..Ord('z'), Ord('A')..Ord('Z'), Ord('1')..Ord('9'), Ord('0')];

{$IFNDEF NEXTGEN}

function SearchBuf(Buf: PAnsiChar; BufLen: Integer; SelStart, SelLength: Integer; SearchString: AnsiString; Options: TStringSearchOptions = [soDown]): PAnsiChar; overload; deprecated 'Moved to the AnsiStrings unit';

{$ENDIF}

{$IFDEF UNICODE}

function SearchBuf(Buf: PChar; BufLen: Integer; SelStart, SelLength: Integer; SearchString: String; Options: TStringSearchOptions = [soDown]): PChar; overload;

{$ENDIF}

{PosEx在S中搜索SubStr,如果找到则返回SubStr的索引位置,否则返回0。如果未给出“偏移”,则结果与调用位置相同。如果指定了“偏移”并且> 1,则搜索从S内的“偏移”位置开始。如果“偏移”大于“长度(S)”,则PosEx返回0。默认情况下,“偏移”等于1。

function PosEx(const SubStr, S: string; Offset: Integer = 1): Integer; inline; overload;

{Soundex函数返回给定字符串的Soundex代码。与原始的Soundex例程不同,此函数可以返回不同长度的代码。此功能大致基于John Midwinter编写的SoundBts。有关Soundex的更多信息,请访问:http://www.nara.gov/genealogy/coding.html此功能背后的一般理论最早于1918年获得专利(US1261167和US1435663),但现在已在公共领域。注意:此功能不会尝试处理“带前缀的名称”的问题。}

type

TSoundexLength = 1..MaxInt;

function Soundex(const AText: string; ALength: TSoundexLength = 4): string;

{SoundexInt使用Soundex,但返回编码为整数的结果Soundex代码。但是,由于整数大小的限制,此功能仅限于八个字符或更少的Soundex代码。DecodeSoundexInt旨在将SoundexInt的结果解码回普通的Soundex代码。长度不是必需的,因为它已编码为SoundexInt的结果。}

type

TSoundexIntLength = 1..8;

function SoundexInt(const AText: string; ALength: TSoundexIntLength = 4): Integer;

function DecodeSoundexInt(AValue: Integer): string;

{SoundexWord是SoundexInt的特殊情况版本,它返回编码成单词的Soundex代码。但是,由于单词大小的限制,此功能使用四个字符的Soundex代码。DecodeSoundexWord旨在将SoundexWord的结果解码回普通的Soundex代码。}

function SoundexWord(const AText: string): Word;

function DecodeSoundexWord(AValue: Word): string;

{SoundexSimilar和SoundexCompare是使用Soundex编码功能的简单比较功能。}

function SoundexSimilar(const AText, AOther: string; ALength: TSoundexLength = 4): Boolean;

function SoundexCompare(const AText, AOther: string; ALength: TSoundexLength = 4): Integer;

{AnsiResemblesText的默认入口点}

function SoundexProc(const AText, AOther: string): Boolean;

type

TCompareTextProc = function(const AText, AOther: string): Boolean;

{如果AnsiResemblesText的默认行为(使用Soundex)不适合您的情况,则可以将其重定向到您自己选择的函数}


var
  ResemblesProc: TCompareTextProc = SoundexProc;
  AnsiResemblesProc: TCompareTextProc = SoundexProc;