function ReturnString(AStr:String;ACount:Integer):String; begin Result:=Copy(AStr,2,ACount); //这里你可以指定ACount不大于3! end;
直接用COPY()函数就可以了,要是源串格式固定的话!
TO FrameSniper(人类不能不吃饭@手机不能不充电.net)
第一楼的程序怎么调试不通呀. [Error] Unit1.pas(19): Undeclared identifier: 'TResultArray' [Error] Unit1.pas(20): Overloaded procedure 'SplitString' must be ed with the 'overload' directive
procedure TForm1.Button1Click(Sender: TObject); var S:WideString; begin S:='@120×2.5'; //其中120是Unicode字符 S:=Copy(S,AnsiPos('@',S)+1,3); ShowMessage(S); end;
或者你自己写代码,先去掉第一个&,然后判断下面的是不是在1到9之间,如果是就取出来,不是就跳出循环 for i:=2 to 字符的长度 如果第i个字符在一到九之间,那么保存在一个字符变量里面 否则就调处for循环!
TO FrameSniper(人类不能不吃饭@手机不能不充电.net) procedure TForm1.Button1Click(Sender: TObject); var S:WideString; begin S:='@120×2.5'; //其中120是Unicode字符 S:=Copy(S,AnsiPos('@',S)+1,3); ShowMessage(S); end; 这个方法是对的.但我说了嘛.像"@25×2.5"的话.取出来就是"25×"了.多了个乘号.
TO FrameSniper(人类不能不吃饭@手机不能不充电.net) "另外,对于你的要求,我现在只是理解为把第一个字符去掉,然后取从第二位开始长度不超过3的子串,不是这样吗?" 对,是从第二位开始取不超过3位,但如果是“"@25×2.5“取3位出来多了个乘号,我只要的是第一个字符到乘号之间的数字(像“@120“单独形式我会取),其它的都不要。行吗?我也知道用ASCII码可以实现,不过,这样不是很麻烦,我是想有没有其它更简单的办法呢?
procedure TForm1.Button1Click(Sender: TObject); var S:WideString; begin S:='@120×2.5'; //其中120是Unicode字符 S:=Copy(S,AnsiPos('@',S)+1,AnsiPos('×',S)-AnsiPos('@',S)); ShowMessage(S); end;
TResultArray = array of string;function SplitString(const source, ch: string): TResultArray;vartemp: string;i: integer;begintemp := source;i := pos(ch, source);while i <> 0 dobeginSetLength(Result, Length(Result) + 1);Result[Length(Result) - 1] := copy(temp, 0, i - 1);delete(temp, 1, i);i := pos(ch, temp);end;SetLength(Result, Length(Result) + 1);Result[Length(Result)-1] := Temp;end;**************function SplitString(const source,ch:string):tstringlist;vartemp:string;i:integer;beginresult:=tstringlist.Create;temp:=source;i:=pos(ch,source);while i<>0 dobeginresult.Add(copy(temp,0,i-1));delete(temp,1,i);i:=pos(ch,temp);end;result.Add(temp);end;调用:s:=splitstring('afsdfsdaaa|bbfdsfsdb|ccc','|');for i:=0 to s.Count-1 dob:=b+s.Strings[i]+#13;showmessage(b);s.free;
begin
Result:=Copy(AStr,2,ACount); //这里你可以指定ACount不大于3!
end;
[Error] Unit1.pas(19): Undeclared identifier: 'TResultArray'
[Error] Unit1.pas(20): Overloaded procedure 'SplitString' must be ed with the 'overload' directive
var
S:WideString;
begin
S:='@120×2.5'; //其中120是Unicode字符
S:=Copy(S,AnsiPos('@',S)+1,3);
ShowMessage(S);
end;
for i:=2 to 字符的长度
如果第i个字符在一到九之间,那么保存在一个字符变量里面
否则就调处for循环!
procedure TForm1.Button1Click(Sender: TObject);
var
S:WideString;
begin
S:='@120×2.5'; //其中120是Unicode字符
S:=Copy(S,AnsiPos('@',S)+1,3);
ShowMessage(S);
end;
这个方法是对的.但我说了嘛.像"@25×2.5"的话.取出来就是"25×"了.多了个乘号.
2、用循环分解并判断每个字符是否0—9的数值;取出前后均为数值的组合;
3、将结果转为数值;
"另外,对于你的要求,我现在只是理解为把第一个字符去掉,然后取从第二位开始长度不超过3的子串,不是这样吗?"
对,是从第二位开始取不超过3位,但如果是“"@25×2.5“取3位出来多了个乘号,我只要的是第一个字符到乘号之间的数字(像“@120“单独形式我会取),其它的都不要。行吗?我也知道用ASCII码可以实现,不过,这样不是很麻烦,我是想有没有其它更简单的办法呢?
var
S:WideString;
begin
S:='@120×2.5'; //其中120是Unicode字符
S:=Copy(S,AnsiPos('@',S)+1,AnsiPos('×',S)-AnsiPos('@',S));
ShowMessage(S);
end;
还能不能解决哦。