Delphi两个取字串长度的函数strlen,length
相关资料:
http://www.delphitop.com/html/zifuchuan/1242.html
https://www.cnblogs.com/xumenger/p/4443955.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.
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我
![](https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_200417075318%E5%8A%A0%E6%88%91QQ.jpg)
![](https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_200417080925%E5%8A%A0%E6%88%91%E5%BE%AE%E4%BF%A1.jpg)