如何把array [] of char 转换成 string,在线等(100分)

  • 主题发起人 主题发起人 ld3235
  • 开始时间 开始时间
L

ld3235

Unregistered / Unconfirmed
GUEST, unregistred user!
我的办法是:<br>Buf &nbsp;:array [0..200]of char;<br>Msg_Content:string;<br><br>for i:=0 to Msg_Length do<br>begin<br> &nbsp; Msg_Content:=Msg_Content+Buf;<br>end; <br>但感觉很不好,期待其他好点简便的方法
 
Buf &nbsp;:array [0..200]of char;<br>把这个改成<br>Buf:string[200];<br>不行么?
 
var<br>Buf &nbsp;:array [0..200]of char;<br>Msg_Content:string;<br>begin<br>buf:='windows';<br>Msg_Content:=buf;<br>showmessage(Msg_Content);<br>............
 
fan2588,不可以<br>41426277,你的转换是错的
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;Buf &nbsp;:array [0..200] of char;<br> &nbsp;Msg_Content:string;<br>begin<br> &nbsp;buf:='aaa';<br> &nbsp;msg_content:=buf;<br> &nbsp;showmessage(msg_content);<br>end;<br><br>测试通过!
 
我试下,可以就给分了
 
带长度copy。。。用这个。<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;Buf &nbsp;:array [0..200] of char;<br> &nbsp;Msg_Content:string;<br>begin<br> &nbsp;buf:='aaa';<br> &nbsp;setlength(Msg_content, length(buf));<br> &nbsp;strCopy(pchar(Msg_content), buf);<br> &nbsp;showmessage(msg_content);<br>end;
 
如果中间不包含#0 则直接赋值即可:<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;ca: array[0..20] of char;<br> &nbsp;I: integer;<br> &nbsp;str: string;<br>begin<br> &nbsp;for I := 0 to 20 do<br> &nbsp;begin<br> &nbsp; &nbsp;ca := 'x';<br> &nbsp;end;<br> &nbsp;Str := ca;<br> &nbsp;ShowMessage(str);<br>end;
 
这是最最快的,而且保证不会丢失 Buff 的内容(楼上那个 #0 后的内容没了,可以看看转换后的长度):<br>var<br> &nbsp;Buf: array [0..200]of char;<br> &nbsp;Msg_Content: string;<br>begin<br> &nbsp;Buf := 'hello';<br><br> &nbsp;SetLength(Msg_Content, Length(Buf));<br> &nbsp;Move(Buf, PChar(Msg_Content)^, Length(Buf));<br><br> &nbsp;ShowMessage(Msg_Content);<br>end;
 
大家试下这个:<br>var<br>Buf &nbsp;:array [0..200]of char;<br>Msg_Content:string;<br>begin<br> &nbsp;buf[0]:=#0;<br> &nbsp;buf[1]:='1';<br> &nbsp;buf[2]:='2';<br> &nbsp;Msg_Content:=midstr(buf,1,2);<br> &nbsp;showMessage(Msg_Content);<br>我只要后面的'12',怎么才能取到后面的'12'
 
LSUPER的方法应该是对的,记得看到过,不过忘了,抽烟回来测试对了就给分
 
给分了,谢谢,呵呵,搞定了
 
晕倒,这样给分的。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
535
import
I
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
后退
顶部