如何从字符串中提取数值
2011-06-02 08:41
369 查看
如何从字符串中提取数值,比如从'-10(16Mn)'中提取10这个数值,当然在字符串中'-'后面
的数值长度不定,而( )中的字符串长度也不定。
来个循环比较看看,条件是:IN(0..9)
str := 'abc056'
s:='';
for i:=1 to length(str) do
begin
if str[i] in ['0'..'9'] then
s:=s+str[i];
end;
http://zhidao.baidu.com/question/210744264.html
pos 加for应该可以提取出来
var s1:string;
i:integer;
begin
s1:='-10(16Mn)';
i:=pos('-',s1);
s1:=copy(s1,i+1,length(s1)-i);
i:=pos('(',s1);
s1:=copy(s1,1,i-1);//这时s1中就是'10'这个字符串了
end;
terry_lzs的方法思路是对的,不过不必过滤负号.
Result := StrToInt(Copy(S, 1, Pos('(', S) - 1);
如果不知道是用'('分隔的话会麻烦些:
b := True;
while b do
try
X := StrToInt(S);
b := False;
except
S := Copy(S, 1, Length(S) - 1);
end;
第二个算法有点问题,改一下:
b := True;
while b do
try
X := StrToInt(S);
b := False;
except
if Length(S) = 0 then Break
else
S := Copy(S, 1, Length(S) - 1);
end;
var
ss:string;
i:integer;
begin
i:=pos('10','-10(16Mn)');
label1.Caption:=copy('-10(16Mn)',i,2);
end;
上面各位的方法都可以,但我想大家可能都忽视了一个从Pascal继承下来,但现在很少用
的函数Val,请看下面的程序
var
s: String;
Code: Integer;
Value: Real;
begin
s := 'ABc-10.22E2(a';
repeat
Val(s, Value, Code);
case Code of
0: //转换成功
ShowMessage(FloatToStr(Value));
1: //开头有非法字符
Delete(s, 1, 1);
else //末尾有非法字符
Delete(s, Code, Length(s));
end;
until (Code = 0) or (Length(s) = 0);
end;
这段程序的优点在于,它不但对于检测整数有效,检测浮点数同样有效。
http://www.itdelphi.com/delphibbs/doc/2001/524238.htm
多谢各位帮助
的数值长度不定,而( )中的字符串长度也不定。
来个循环比较看看,条件是:IN(0..9)
str := 'abc056'
s:='';
for i:=1 to length(str) do
begin
if str[i] in ['0'..'9'] then
s:=s+str[i];
end;
http://zhidao.baidu.com/question/210744264.html
pos 加for应该可以提取出来
var s1:string;
i:integer;
begin
s1:='-10(16Mn)';
i:=pos('-',s1);
s1:=copy(s1,i+1,length(s1)-i);
i:=pos('(',s1);
s1:=copy(s1,1,i-1);//这时s1中就是'10'这个字符串了
end;
terry_lzs的方法思路是对的,不过不必过滤负号.
Result := StrToInt(Copy(S, 1, Pos('(', S) - 1);
如果不知道是用'('分隔的话会麻烦些:
b := True;
while b do
try
X := StrToInt(S);
b := False;
except
S := Copy(S, 1, Length(S) - 1);
end;
第二个算法有点问题,改一下:
b := True;
while b do
try
X := StrToInt(S);
b := False;
except
if Length(S) = 0 then Break
else
S := Copy(S, 1, Length(S) - 1);
end;
var
ss:string;
i:integer;
begin
i:=pos('10','-10(16Mn)');
label1.Caption:=copy('-10(16Mn)',i,2);
end;
上面各位的方法都可以,但我想大家可能都忽视了一个从Pascal继承下来,但现在很少用
的函数Val,请看下面的程序
var
s: String;
Code: Integer;
Value: Real;
begin
s := 'ABc-10.22E2(a';
repeat
Val(s, Value, Code);
case Code of
0: //转换成功
ShowMessage(FloatToStr(Value));
1: //开头有非法字符
Delete(s, 1, 1);
else //末尾有非法字符
Delete(s, Code, Length(s));
end;
until (Code = 0) or (Length(s) = 0);
end;
这段程序的优点在于,它不但对于检测整数有效,检测浮点数同样有效。
http://www.itdelphi.com/delphibbs/doc/2001/524238.htm
多谢各位帮助
相关文章推荐
- 字符串中如何提取数值
- 【性能】乙 Spads 出品字符串高效提取数值方法 Shane 末日圣诞奉献
- 如何:在十六进制字符串与数值类型之间转换(C# 编程指南)
- Matlab如何在同一行中混合显示字符串和数值变量
- 【原】如何将十六进制字符串转换为数字(数值)
- [转]在sqlserver中如何从字符串中提取数字,英文,中文,过滤重复字符
- (C# 正则表达式)判断匹配, 提取字符串或数值
- 如何提取Listview的数值
- 字符串提取数值-写网友的简单测试
- perl字符串如何提取数字
- MySQL如何有效的存储IP地址及字符串IP和数值之间如何转换
- ESP8266-从字符串中提取数值的函数
- 如何:在十六进制字符串与数值类型之间转换(C# 编程指南MSDN)
- 如何将字符串或者数值转成枚举对象
- web数据采集核心技术分享系列(二)如何提取信息?字符串?正则?xpath?xslt?自定义?...什么才是王道?
- Java从字符串中提取整数、浮点型数值
- oracle中如何判断字符串是否全为数字,以及从任意字符串中提取数字
- 在sqlserver中如何从字符串中提取数字,英文,中文,过滤重复字符
- C#中如何从字符串中提取数字
- 如何将十六进制字符串转换为数字(数值)