- 人气:
- 放大
- 缩小
- 二维码
- 赞赏
delphi将N个相同字符提取到左边,M个相同字符提取到右边
比如:我有个字符串为1001110011101101011101100,我想把所有的1放在左边,所有的0放在右边,得到一个新的字符串,怎么办呢?
上代码:
uses
StrUtils;
procedure TForm1.Button1Click(Sender: TObject);
var
i,count0,count1:integer;
s,str:string;
begin
Count0 := 0;
Count1 := 0;
s:='1001110011101101011101100';
for I := 1 to Length(s) do
begin
if s[I] = '0' then Inc(Count0);
if s[I] = '1' then Inc(Count1);
end;
str:=DupeString('1',count1) + DupeString('0',count0);
ShowMessage(str); //得到结果1111111111111110000000000
end;
这里说下DupeString函数,要uses StrUtils单元,DupeString(const AText: string; ACount: Integer)反复字符串函数,第一个参数为要反复的字符串,第二个参数为反复的个数。