zoukankan      html  css  js  c++  java
  • string系列操作1

    Delphi string、char数组和Pchar


    Pchar:是指针,占4B,定义时指向随即地址,由#0表示字符串结尾。
    Pchar指向内存,只是代表一个地址,可随意指向任何东西。

    Char数组:长度和地址比较固定。SetLength,地址不可被赋值。

    string:分两种,内可包含#0。
    ShortString:最长256字节
    AnsiString:最长256的4次方

    String-->PChar:只是讲String中字符串的地址复制给了Pchar。

    Pchar和char数组,占内存少,速度快,效率高


    转换:

    1、将指针型数据转换为字符串:
    function StrPas(const Str: PChar): string;
    eg:
    var
    mystr:array[0..20] of char;
    begin
    mystr:='我的程序';
    showmessage(strpas(@mystr));
    end;
    注意:指向字符串的指针,如果不是以0结尾,运行时会出现错误:
    {s[0]=3 s[1]='n' s[2]='e' s[3]='w'}
    var
      s:string;
    p:pchar;
    begin
     s:='new';
    label1.caption:=s; {new}
     label2.caption:=intTostr(integer(s[0]));{3是字符串的长度}
     p:=@s[1];{不是以0结尾,莫用pchar型指针}
    label3.caption:=strpas(p); {运行时出现错误}
    end;
    例2:在字符串结尾人工加入0即char(0),可使用指向字符串的指针。
      {s[0]=4 s[1]='n' s[2]='e' s[3]='w' s[4]=0;}
    var
    s:string;
    p:pchar;
    begin
     p:=@s[1];
     s:='new'+char(0); {以0结尾,可用pchar型指针}
     label1.caption:=strpas(p); {new}
    end;

    2、将string转化为PChar或数组:
    function StrPCopy(Dest: PChar; const Source: string): PChar


    3、procedure GetMem(var P:pointer;Size:integer);

  • 相关阅读:
    企业级 SpringBoot 教程 (九)springboot整合Redis
    03 网格系统
    02 表单
    01 排版
    客户端调用webSerices
    sql 一行转多行
    sql 多行转一行
    时间差计算 Stopwatch
    sql 游标
    Linq连接查询
  • 原文地址:https://www.cnblogs.com/760044827qq/p/4183625.html
Copyright © 2011-2022 走看看