其他分享
首页 > 其他分享> > Delphi两个取字串长度的函数strlen,length

Delphi两个取字串长度的函数strlen,length

作者:互联网

相关资料:

http://www.delphitop.com/html/zifuchuan/1242.html

 

【strlen】PChar 串专用函数:串长度

procedure TForm1.BitBtn1Click(Sender: TObject);
var s: Widestring;
begin
s := '桂枝香在故国晚秋';
ShowMessage(IntToStr(Length(String(s))));//结果不是8而是16
end;
━━━━━━━━━━━━━━━━━━━━━━━━━━
如果想得到8:
ShowMessage(IntToStr(Length(s)));//结果不是16而是8

━━━━━━━━━━━━━━━━━━━━━━━━━━
【length】Length (S): Integer; 返回参数字符串的长度。

  AnsiString这是Pascal缺省的字符串类型,它由AnsiChar字符组成,其长度没有限制,同时与null结束的字符串相兼容。

  在大多数情况下,AnsiString类型能被用成PChar,应该尽可能地使用AnsiString,因为它对字符串内存的管理是自动,极大地减少了应用程序中内存混乱的错误代码,因此,要尽可能地避免用PChar类型以及对它相应进行人工分配内存。

 

 

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var s: Widestring;
begin
s := '桂枝香在故国晚秋';
ShowMessage(IntToStr(Length(String(s))));//XE显示8   //D7 结果不是8而是16
end;

procedure TForm1.Button2Click(Sender: TObject);
var s: Widestring;
begin
s := '桂枝香在故国晚秋';
ShowMessage(IntToStr(Length(s))); //XE显示8  //D7结果不是16而是8
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  s: AnsiString;
begin
  s:='桂枝香在故国晚秋';
  ShowMessage(IntToStr(Length(s)));  //XE显示16
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  s: AnsiString;
begin
  s:='桂枝香在故国晚秋';
  ShowMessage(IntToStr(strlen(PWideChar(s)))); //XE显示8
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  s: AnsiString;
begin
  s:='桂枝香在故国晚秋';
  ShowMessage(IntToStr(SizeOf(PWideChar(s)))); //XE显示4(32位程序)
end;

end.

  

标签:end,Sender,Delphi,TForm1,TObject,IntToStr,length,strlen,procedure
来源: https://www.cnblogs.com/FKdelphi/p/15042413.html