1. 1.copy(str,pos,num) 从str字符串的pos处开始,截取num个字符的串返回.

  2.          假设str为'abcdef',copy(str,3,2)='cd',copy(str,4,10)='def'

  3. 2.concat(str1,str2{,strn}) 把各自变量连接起来,返回连接后的字符串(长度不能超过255)

  4. 3.length(str)      返回str的字符个数,即其长度.

  5. 4.pos(obj,target)   在target字符串中找出第一个出现obj的第一个字符位置,如果找不到,返回0.

  6. 5.AnsiStrLastChar('你好')结果是“好”。如果有半个汉字出现,返回这半个汉字。二者字符串长度分别为2和1。

  7. 6.CompareStr  - 区分大小写

  8. 7.CompareText - 不区分大小写

  9. 8.StringReplace(const S, OldPattern, NewPattern: string;Flags: TReplaceFlags): string;

  10. 字符串替换函数,需要引用SysUtils单元

  11. rfReplaceAll:全部替换  
    rfIgnoreCase:忽略大小写

  12. 使用方法Lg:

  13.   str:='01231142211 :655767';//需要把:替换成----
       s:=StringReplace(str,':','----',[rfReplaceAll]);

  14. 从后向前读取字符串

    function GetUrlLast(Source,target : string): string;
    var
      sl: TStrings;
    begin
      sl:= TStringList.Create;
      try
        sl.Delimiter:= target[1];//把字符串的Delimiter属性设为你想要的 这里为/
        sl.DelimitedText:= Source ;
        result:= sl.Strings[sl.Count-1];
      finally
         sl.Free;
      end;


  15. For Example:
    
    var
    aStr: String;
    begin
    aStr := 'This is a book, not a pen!';
    ShowMessage(StringReplace (aStr, 'a', 'two', []));   //This is two book, not a pen!只替换了第一个符合的字
    ShowMessage(StringReplace (aStr, 'a', 'two', [rfReplaceAll]));
     //This is two book, not two pen!替换了所有符合的字
    aStr := 'This is a book, not A pen!';
    ShowMessage(StringReplace (aStr, 'a', 'two', [rfReplaceAll]));   //This is two book, not A pen!只替换了符合的字(小写a)
    ShowMessage(StringReplace (aStr, 'a', 'two', [rfReplaceAll, rfIgnoreCase]));   //This is two book, not two pen!不管大小写替换了所有符合的字end;
    
    9.delete 是删除一个 字符串中的 某部分字符用法是 delete(str,//被删除的字符串 
    index,//从第几个字符开始删除
    count //删除几个
                 );
    Delete(S, 2, 2); 就是从S中的第二个开始删除,删除2个字符即2、3.所以结果是145.
  16. LEFTSTR, MIDSTR, RIGHTSTR的介绍

  17. 这几个函数都包含在StrUtils中,所以需要uses StrUtils;
    假设字符串是 Dstr := ’Delphi is the BEST’, 那么

  18. LeftStr(Dstr, 5) := ’Delph’
    MidStr(Dstr, 6, 7) := ’i is th’
    RightStr(Dstr, 6) := ’e BEST’


  19. 8.{ 判断字符是否是数字}

  20. functionIsDigit(ch: char): boolean;

  21. begin

  22.  Result := ch in ['0'..'9'];

  23. end;


  24. 9、{ 判断字符是否是大写字符}

  25. functionIsUpper(ch: char): boolean;

  26. begin

  27.  Result := ch in ['A'..'Z'];

  28. end;

  29. 10、{ 判断字符是否是小写字符}

  30. functionIsLower(ch: char): boolean;

  31. begin

  32.  Result := ch in ['a'..'z'];

  33. end;

  34. 11、{ 转换为大写字符}

  35. functionToUpper(ch: char): char;

  36. begin

  37.  Result := chr(ord(ch) and $DF);

  38. end;

  39. 12、{ 转换为小写字符}

  40. functionToLower(ch: char): char;

  41. begin

  42.  Result := chr(ord(ch) or $20);

  43. end;

  44. { Capitalizes first letter of every word in s }


  45. function Proper(const s: string): string;

  46. var

  47.  i: Integer;

  48.  CapitalizeNextLetter: Boolean;

  49. begin

  50.  Result := LowerCase(s);

  51.  CapitalizeNextLetter := True;

  52. for i := 1 to Length(Result) do

  53. begin

  54.    if CapitalizeNextLetter and IsLower(Result[i]) then

  55.      Result[i] := ToUpper(Result[i]);

  56.    CapitalizeNextLetter := Result[i] = ' ';

  57. end;

  58. end;

  59. 13.{ 返回两个子字符串之间字符的个数}

  60. Functionp2pcount( s, ss1, ss2 : string ): integer;

  61. var i, j, slen : integer;

  62. begin

  63.   i := pos( ss1, s );

  64.   j := pos( ss2, s );

  65.   slen := Length(ss2);

  66.   if j >= i then Result := j - i + slen else Result := 0;

  67. end;

  68. 14.{ 更快速的字符查询,快40%}

  69. functionScanStr(ToScan: PChar; Sign: Char):PChar;

  70. begin

  71.  Result:= nil;

  72.  if ToScan <> nil then

  73.    while (ToScan^ <> #0) do begin

  74.      if ToScan^ = Sign thenbegin

  75.        Result:= ToScan;

  76.        break;

  77. end;

  78.     inc(ToScan);

  79. end;

  80. end;

  81. /

  82. 15.替换字符串中子串的函数,他可以从字符串中找出指定子串,并替换为另一子串

  83. function replacing(S,source,target:string):string;

  84. var site,StrLen:integer;

  85. begin

  86. {source在S中出现的位置}

  87. site:=pos(source,s);

  88. {source的长度}

  89. StrLen:=length(source);

  90. {删除source字符串}

  91. delete(s,site,StrLen);

  92. {插入target字符串到S中}

  93. insert(target,s,site);

  94. {返回新串}

  95. replacing:=s;

  96. end;

  97. ///

  98. 另两个替换字符串中子串的函数

  99. function repl_substr( sub1, sub2, s: string ): string;

  100. var i: integer;

  101. begin

  102.   repeat

  103.     i := pos( sub1, s ) ;

  104.     if i > 0 thenbegin

  105. delete( s, i, Length(sub1));

  106. insert( sub2, s, i );

  107. end;

  108.   until i < 1;

  109.   Result := s;

  110. end;

  111. function ReplaceText(const S,ReplacePiece,ReplaceWith: String):String;

  112. Var Position: Integer;

  113.    TempStr: String;

  114. begin

  115.  Position := Pos(ReplacePiece,S);

  116.  if Position > 0 thenBegin

  117.    TempStr := S;

  118. Delete(TempStr,1,Position-1+Length(ReplacePiece));

  119.    Result :=

  120. Copy(S,1,Position-1)+ReplaceWith+ReplaceText(TempStr,ReplacePiece,ReplaceWith)

  121. Endelse Result := S;

  122. end;

  123. 替换全部子字符串的函数

  124. function ReplaceSub(str, sub1, sub2: String): String;

  125.    var

  126.    aPos: Integer;

  127.    rslt: String;

  128. begin

  129.    aPos := Pos(sub1, str);

  130.    rslt := '';

  131.    while (aPos <> 0) do begin

  132.      rslt := rslt + Copy(str, 1, aPos - 1) + sub2;

  133. Delete(str, 1, aPos + Length(sub1));

  134.      aPos := Pos(sub1, str);

  135. end;

  136.    Result := rslt + str;

  137. end;

  138. /

  139. 在字符串左右填充指定数量的指定字符

  140. function UT_PadString(inString :string; maxLength :integer; padChar :char;

  141. left :boolean) :string;

  142. begin

  143.  result := inString;

  144.  while (Length(result) < maxLength) do

  145.    if (left) then

  146.      result := padChar + result

  147. else

  148.      result := result + padChar;

  149. end;

  150. /

  151. 提取字符串中指定子字符串前的字符串

  152. Function Before ( string ; Var S:string ) : string ; < /span>

  153.  Var

  154.  F : Word ;

  155. begin

  156.  F := POS (Src,S) ;

  157.  if F=0 then

  158.    Before := S

  159. else

  160.    Before := COPY(S,1,F-1) ;

  161. end ;

  162. //

  163. 提取字符串中指定子字符串后的字符串

  164. FunctionAfter ( string ; Var S:string ) : string ; < /span>

  165.  Var

  166.  F : Word ;

  167. begin

  168.  F := POS (Src,S) ;

  169.  if F=0 then

  170. After := ''

  171. else

  172. After := COPY(S,F+length(src),length(s)) ;

  173. end ;

  174. 判断字符串是否可以转换为整数

  175. function IsIntStr(const S: string): boolean;

  176. begin

  177.  Result:=StrToIntDef(S,0)=StrToIntDef(S,1);

  178. end;

  179. //

  180. 从字符串中删除指定字符串

  181. procedure RemoveInvalid(what, where: string): string;

  182.  var

  183.  tstr: string;

  184. begin

  185.  tstr:=where;

  186.  while pos(what, tstr)>0 do

  187.    tstr:=copy(tstr,1,pos(what,tstr)-1) +

  188.       copy(tstr,pos(what,tstr)+length(tstr),length(tstr));

  189.  Result:=tstr;

  190. end;

  191. 用法:

  192.  NewStr:=RemoveInvalid('','This is my string and I wan to

  193.       remove the word ');

  194. ///

  195. 根据某个字符分割字符串的函数

  196. procedure SeparateTerms(s : string;Separator : char;Terms : TStringList);

  197. { This browses a string and divide it into terms whenever the given

  198.  separator is found. The separators will be removed }

  199.  var

  200.  hs : string;

  201.  p : integer;

  202. begin

  203.  Terms.Clear; // First remove all remaining terms

  204.  if Length(s)=0 then   // Nothin' to separate

  205.    Exit;

  206.  p:=Pos(Separator,s);

  207.  while P<>0 do

  208. begin

  209.    hs:=Copy(s,1,p-1);   // Copy term

  210.    Terms.Add(hs);       // Addto list

  211. Delete(s,1,p);       // Remove term and separator

  212.    p:=Pos(Separator,s); // Search next separator

  213. end;

  214.  if Length(s)>0 then

  215.    Terms.Add(s);        // Add remaining term

  216. end;

  217. ==========

  218. = 用  法

  219. ==========

  220. var

  221. Terms : TStringList;

  222. i : integer;

  223. const

  224. TestStr = '1st term;2nd term;3rd term';

  225. begin

  226.  Terms:=TStringList.Create;

  227.  SeparateTerms(TestStr,';',Terms);

  228. for i:=0 to terms.Count-1 do

  229.    ShowMessage(Terms.Strings[i]);

  230.  Terms.Free;

  231. end;

  232. /

  233. 根据一组字符分割字符串的函数

  234. type

  235. Charset = setofChar;

  236. var

  237. f : Text;

  238. s : String;

  239. procedure WriteStringSplitted(var s: String; Separators: Charset);

  240. var

  241. a,e : Integer;  {anfang und ende des w鰎tchens}

  242. begin

  243. a := 1;

  244. for e := 1 to Length(s) do

  245.  if s[e] in Separators thenbegin

  246.   WriteLn(Copy(s, a, e-a));

  247.   a := e + 1;

  248. end;

  249.  WriteLn(Copy(s, a, e-a+1));

  250. end;

  251. begin

  252. Assign(f, 'c:/dingsbums/text.txt');

  253. Reset(f);

  254. while not EOF(f) do begin

  255.  ReadLn(f,s);

  256.  WriteStringSplitted(s, [':', ',']);

  257. end;

  258. Close(f);

  259. end.

  260. //

  261. {===============================================================}

  262. { 函数  : RESULTSTRING = HexToBin(HEXSTRING)

  263. { 目的   : 把十六进制字符串转换为二进制字符串

  264. {

  265. {===============================================================}

  266. { 函数  : RESULTINTEGER = HexCharToInt(HEXCHAR)

  267. { 目的   : 转换一个十六进制字符为整数

  268. {===============================================================}

  269. { 函数  : RESULTSTRING = HexCharToBin(HEXCHAR)

  270. { 目的   : 转换一个十六进制字符为二进制字符串

  271. {===============================================================}

  272. { 函数  : RESULTINTEGER = Pow(BASE,POWER)

  273. { 目的   : 指数函数

  274. {===============================================================}

  275. { 函数  : RESULTINTEGER = BinStrToInt(BINSTRING)

  276. { 目的   : 把二进制字符串转换为整数

  277. {===============================================================}

  278. { 函数  : RESULTSTRING = DecodeSMS7Bit (PDUSTRING)

  279. { 目的   : 解码一个7-bit SMS (GSM 03.38) 为ASCII码

  280. {===============================================================}

  281. { 函数  :  RESULTSTRING = ReverseStr (SOURCESTRING)

  282. { 目的   : 反转一个字符串

  283. {===============================================================}

  284. unit BinHexTools;

  285. interface

  286. function HexToBin(HexNr : string): string;

  287. function HexCharToInt(HexToken : char):Integer;

  288. function HexCharToBin(HexToken : char): string;

  289. function pow(base, power: integer): integer;

  290. function BinStrToInt(BinStr : string) : integer;

  291. function DecodeSMS7Bit(PDU : string):string;

  292. function ReverseStr(SourceStr : string) : string;

  293. implementation

  294. uses sysutils, dialogs;

  295. function HexCharToInt(HexToken : char):Integer;

  296. begin

  297.  {if HexToken>#97 then HexToken:=Chr(Ord(HexToken)-32);

  298.  { use lowercase aswell }

  299.  Result:=0;

  300.  if (HexToken>#47) and (HexToken<#58) then       { chars 0....9 }

  301.     Result:=Ord(HexToken)-48

  302. else if (HexToken>#64) and (HexToken<#71) then  { chars A....F }

  303.     Result:=Ord(HexToken)-65 + 10;

  304. end;

  305. function HexCharToBin(HexToken : char): string;

  306. var DivLeft : integer;

  307. begin

  308.    DivLeft:=HexCharToInt(HexToken);   { first HEX->BIN }

  309.    Result:='';

  310.                                       { Use reverse dividing }

  311.    repeat                             { Trick; divide by 2 }

  312.      if odd(DivLeft) then             { result = odd ? thenbit = 1 }

  313.        Result:='1'+Result             { result = even ? thenbit = 0 }

  314. else

  315.        Result:='0'+Result;

  316.      DivLeft:=DivLeft div 2;       { keep dividing till 0 leftand length = 4 }

  317.    until (DivLeft=0) and (length(Result)=4);      { 1 token = nibble = 4 bits }

  318. end;

  319. function HexToBin(HexNr : string): string;

  320. { only stringsize is limit of binnr }

  321. var Counter : integer;

  322. begin

  323.  Result:='';

  324. for Counter:=1 to length(HexNr) do

  325.    Result:=Result+HexCharToBin(HexNr[Counter]);

  326. end;

  327. function pow(base, power: integer): integer;

  328. var counter : integer;

  329. begin

  330.  Result:=1;

  331. for counter:=1 to power do

  332.    Result:=Result*base;

  333. end;

  334. function BinStrToInt(BinStr : string) : integer;

  335. var counter : integer;

  336. begin

  337.  if length(BinStr)>16 then

  338.    raise ERangeError.Create(#13+BinStr+#13+

  339. 'is not within the valid range of a 16 bit binary.'+#13);

  340.  Result:=0;

  341. for counter:=1 to length(BinStr) do

  342.      if BinStr[Counter]='1'then

  343.        Result:=Result+pow(2,length(BinStr)-counter);

  344. end;

  345. function DecodeSMS7Bit(PDU : string):string;

  346. var OctetStr : string;

  347.    OctetBin : string;

  348.    Charbin  : string;

  349.    PrevOctet: string;

  350.    Counter  : integer;

  351.    Counter2 : integer;

  352. begin

  353.  PrevOctet:='';

  354.  Result:='';

  355. for Counter:=1 to length(PDU) do

  356. begin

  357.      if length(PrevOctet)>=7 then     { if 7 Bit overflow on previous }

  358. begin

  359.          if BinStrToInt(PrevOctet)<>0 then

  360.            Result:=Result+Chr(BinStrToInt(PrevOctet))

  361. else Result:=Result+' ';

  362.          PrevOctet:='';

  363. end;

  364.      if Odd(Counter) then            { only take two nibbles at a time }

  365. begin

  366.          OctetStr:=Copy(PDU,Counter,2);

  367.          OctetBin:=HexToBin(OctetStr);

  368.          Charbin:='';

  369. for Counter2:=1 to length(PrevOctet) do

  370.            Charbin:=Charbin+PrevOctet[Counter2];

  371. for Counter2:=1 to 7-length(PrevOctet) do

  372.            Charbin:=OctetBin[8-Counter2+1]+Charbin;

  373.          if BinStrToInt(Charbin)<>0 then Result:=Result+Chr(BinStrToInt(CharBin))

  374. else Result:=Result+' ';

  375.          PrevOctet:=Copy(OctetBin,1,length(PrevOctet)+1);

  376. end;

  377. end;

  378. end;

  379. function ReverseStr(SourceStr : string) : string;

  380. var Counter : integer;

  381. begin

  382.  Result:='';

  383. for Counter:=1 to length(SourceStr) do

  384.    Result:=SourceStr[Counter]+Result;

  385. end;

  386. end.