作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
procedure Split(S: String; List: TStringList; Separator: Char);
var
P, C: PAnsiChar;
S, Buff: String;
begin
List.Clear;
if S = '' then
Exit;
List.BeginUpdate;
(* [Ajusting size - Slow *)
if S[1] = Separator then
Insert('', S, 1);
S := S + Separator;
(* Adjusting size] *)
//Get Pointer to data
P := PChar(S);
//initial position
C := P;
while P^ <> #0 do //check if reached the end of the string
begin
//when found a separator
if P^ = Separator then
begin
if P = C then //check if the slot is empty
Buff := ''
else //when it is not empty, make an string buffer
SetString(Buff, C, P-C);
List.Add(Buff); //add the string into the list
Inc(C, P-C+1); //moves the pointer C to the adress of the pointer P
end;
Inc(P); //go to next char in the string
end;
List.EndUpdate;
end;
if S[1] = Separator then
begin
SetLength(Str, Length(S)+2);
//HERE!! how to copy the string
Str[1] := ' ';
end
else
begin
SetLength(Str, Length(S)+1);
//HERE!! how to copy the string
end;
//Add Separator in the last position
Str[Length(Str)] := Separator;
最佳答案
像这样:
if S[1] = Separator then
begin
SetLength(Str, Length(S)+2);
Move(Pointer(S)^, Str[2], Length(S)*SizeOf(Char));
S[1] := ' '; // surely you mean Str[1] := ' '
end
else
begin
SetLength(Str, Length(S)+1);
Move(Pointer(S)^, Str[1], Length(S)*SizeOf(Char));
end;
//Add Separator in the last position
Str[Length(Str)] := Separator;
var
dest: PChar;
if S[1] = Separator then
begin
SetLength(Str, Length(S)+2);
dest := @Str[2];
S[1] := ' '; // surely you mean Str[1] := ' '
end
else
begin
SetLength(Str, Length(S)+1);
dest := @Str[1];
end;
Move(Pointer(S)^, dest^, Length(S)*SizeOf(Char));
//Add Separator in the last position
Str[Length(Str)] := Separator;
关于string - 如何将一个字符串复制到另一个字符串中,使字符串保留在第一个位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17470541/
我是一名优秀的程序员,十分优秀!