按自定义长度截取字符串

按自定义长度截取字符串,运行结果如下:

按自定义长度截取字符串

 

 

代码如下:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,Math;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  S,SS01,SS02,SS03,SS04:string;
  I:Integer;
  function STR (SS:string;I:Integer):string;
  var
    J,K,h:Integer;
    MYARRAY:array of string;
  begin
    K:=Ceil(Length(SS)/I);
    for h:=1 to k do
    begin
      begin
        SS02:=Copy(SS,I+1,Length(SS));
        SS01:=StringReplace(SS,SS02,‘‘,[rfReplaceAll, rfIgnoreCase]);
        SS:=SS02;
        SS04:=SS01;
        J:=Length(SS02);
        {
        IF Length(SS02)>I then
        begin
          //SS03:=STR(SS02,I);
          //SS:=SS02;
          //SS02:=Copy(SS,I+1,Length(ss));//lenth函數費時,速度慢,長度多了4byte。
          SS02:=Copy(SS,I+1,1000000);//這樣會快很多
          //SS01:=StringReplace(SS,SS02,‘ ‘,[rfReplaceAll, rfIgnoreCase]);
        end;
        }
      end;
      //SS03:=SS03+#13#10+SS04;//回車鍵加在前面
      SS03:=SS03+SS04+#13#10;//回車鍵加在後面
    end;
    Result:=SS03;
  end;
begin
  //SS:=StringReplace(‘123456‘,#13,‘‘,[rfReplaceAll, rfIgnoreCase]);//#13是SqlServer的換行方式
 { SS:=‘ABCDEFGHIJK‘;
  I:=Length(SS)div 4;
  if I>1 then
  begin
    SS02:=Copy(SS,5,Length(SS));
    SS01:=StringReplace(SS,SS02,‘ ‘,[rfReplaceAll, rfIgnoreCase]);
    S:=SS01+#13+SS02;
  end; }
  S:= STR(ABCDEFGHIJKLMN,4);
  ShowMessage(S);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  ss:string;
begin
  ss:=i am chinese!;//13個字符,包括空格。
  ShowMessageFmt(%d,[SizeOf(ss)]);//4,此處為4字節,說明delphi中的string是一個指針!
  ShowMessageFmt(%d,[Length(ss)]);//13,取到ss里保存的字符个数
end;

end.

 

按自定义长度截取字符串

上一篇:最短路输出路径


下一篇:go接收前端传过来的方式