打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
数组与字符串转换

数组与字符串转换

其实规则很简单:

  • Move时访问的是string或string[n],要用string[1],因为string[0]是字符串的长度,所以所有string都以string[1]开始。
  • Move时访问的是array,静态array(array[0..n] of Char)时直接使用,动态array(array of Char)时使用array[0]。因为动态array时变量名其实只是一个地址。
  • Move时访问的是PChar,要使用^取指向的内容。

在Move前,如果目标是数组的话:

  1. 静态数组array[0..n] of Char,取两者最小长度直接Move
  2. 动态数组array of Char,取string长度并把数组SetLength,Move

 如果目标是string,无论是string或string[n]都要先SetLength。

如果目标是PChar,要用GetMemory申请内存,使用完后可别忘了FreeMemory。

简单的一点测试代码,我没有添加结果输出,自己用F8跟踪看结果吧:

// 字符串到数组的转换
procedure StringToArray;
var
  s1: string;
  s2: string[20];
  buf1: array[0..19] of Char;
  buf2: array of Char;

  len1, len2: Integer;

  procedure cls;
  begin
    s1 := '';
    s2 := '';
    buf1 := '';
    SetLength(buf2, 0);
    SetLength(buf2, 20);
    len1 := 0;
    len2 := 0;
  end;

begin
  // string to char array
  cls;
  s1 := 'http://MyvNet.com';
  len1 := Length(s1);
  len2 := Length(buf1);
  Move(s1[1], buf1, Min(len1, len2));

  // string to dynamic char array
  cls;
  s1 := 'http://MyvNet.com';
  len1 := Length(s1);
  len2 := Length(buf2);
  Move(s1[1], buf2[0], Min(len1, len2));

  // string array to char array
  cls;
  s2 := 'http://MyvNet.com';
  len1 := Length(s2);
  len2 := Length(buf1);
  Move(s2[1], buf1, Min(len1, len2));

  // string array to dynamic char array
  cls;
  s2 := 'http://MyvNet.com';
  len1 := Length(s2);
  len2 := Length(buf2);
  Move(s2[1], buf2[0], Min(len1, len2));

  cls;
end;

 // 数组到字符串的转换
procedure ArrayToString;
var
  s1: string;
  s2: string[20];
  buf1: array[0..19] of Char;

  len1, len2: Integer;

  procedure cls;
  begin
    s1 := '';
    s2 := '';
    buf1 := '';
    SetLength(buf2, 0);
    SetLength(buf2, 20);
    len1 := 0;
  end;

begin
  // char array to string
  cls;
  buf1 := 'http://MyvNet.com';
  len1 := Length(buf1);
  SetLength(s1, len1);
  Move(buf1, s1[1], len1);

  // char array to string array
  cls;
  buf1 := 'http://MyvNet.com';
  len1 := Length(buf1);
  SetLength(s2, len1);
  Move(buf1, s2[1], len1);
  // or
  //Move(buf1, s2[1], len1);
  //s2[0] := Chr(len1);

  // dynamic char array to string
  cls;
  buf1 := 'http://MyvNet.com';
  Move(buf1, buf2[0], Length(buf1));
  len1 := Length(buf2);
  SetLength(s1, len1);
  Move(buf2[0], s1[1], len1);

  // dynamic char array to string array
  cls;
  buf1 := 'http://MyvNet.com';
  Move(buf1, buf2[0], Length(buf1));
  len1 := Length(buf2);
  SetLength(s2, len1);
  Move(buf2[0], s2[1], len1);
  // or
  //Move(buf2[0], s2[1], len1);
  //s2[0] := Chr(len1);

  cls;
end;

// PChar和数组的转换
procedure TForm1.PCharAndArray;
var
  pc: PChar;
  buf1: array[0..19] of Char;
  buf2: array of Char;
  len1: Integer;

  procedure cls;
  begin
    pc := '';
    buf1 := '';
    SetLength(buf2, 0);
    SetLength(buf2, 20);
    len1 := 0;
  end;

begin
  // PChar to array
  cls;
  pc := 'http://MyvNet.com';
  len1 := Length(pc);
  Move(pc^, buf1, len1);

  // PChar to dynamic array
  cls;
  pc := 'http://MyvNet.com';
  len1 := Length(pc);
  Move(pc^, buf2[0], len1);

  // array to PChar
  cls;
  buf1 := 'http://MyvNet.com';
  len1 := Length(buf1);
  pc := GetMemory(len1);
  Move(buf1, pc^, len1);
  FreeMemory(pc);

  // dynamic array to PChar
  cls;
  buf1 := 'http://MyvNet.com';
  Move(buf1, buf2[0], Length(buf1));
  len1 := Length(buf2);
  pc := GetMemory(len1);
  Move(buf2[0], pc^, len1);
  FreeMemory(pc);

  cls;
end;

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Delphi数组的使用
【delphi】实现微信公众号,小程序消息加密解密函数
delphi 简体和繁体字符串转换
跨平台 C++ Http Get,Post函数
美团面试题:String s = new String("111")会创建几个对象?
[QT]QByteArray与char、int、float(及其数组)、string之间的互相转化
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服