Home > Article > Web Front-end > Extract numbers from Js string
This article mainly shares with you examples of extracting numbers from Js strings, hoping to help everyone.
One parseInt() method:
The first thing that comes to mind is the parseInt method provided by js, example:
var str ="4500元"; var num = parseInt(str); alert(num);//4500
The result is what we want. If you think it’s that simple, you’d be wrong. If there are non-numeric characters in front of the string, the above method will not work:
var str ="价格:4500元"; var num = parseInt(str); alert(num);//NaN
In this example, NaN will pop up, so solve it The simplest way to solve this problem is: if you know the string format, remove the preceding non-characters. In the above example, removing the substring "Price:"
var str ="价格:4500元"; var num = parseInt(str.substring(1).substring(1).substring(1)); alert(num);//4500
Obviously, this is more troublesome. In addition, there is also the parseInt() method in the Java language. As long as there are non-digits in the string passed there, myeclipse will prompt an error. The parseInt() method in JS can pass non-numeric strings. As long as the string is in front of it, it will still run until it encounters non-numeric characters and stop. For example, consider the following example.
var str ="4500元,等级:2"; var num = parseInt(str); alert(num);//4500
No error will be reported, and the result is still the same, because the system stops when it finds "yuan", no matter whether there are numbers after it, it will not extract it anymore . So the result of 45002 will not appear. There are many such examples in JS. For example, if /g is not written in the regular expression, the default search for the first substring that matches will jump out and will not proceed further. NaN appears in the second example of this article for this reason, please understand it carefully.
Regarding the parseInt() method, there can also be a second parameter. The second parameter represents the base of the first parameter. Take an example:
parseInt("11", 2); // 结果:3 如果想把一个二进制数字字符串转换成整数值,只要把第二个参数设置为 2 就可以了。
If the latter parameter is not written, it will be converted to decimal by default.
The last question is, what does the parse function do? All strings (text type) are passed between programs, and they must be converted into the required type when used. The parse function converts the string into the type we need, such as parseInt(), $.parseHTML().
2. Regularity
I mentioned regularity earlier. In fact, regular processing is relatively simple, just replace non-numeric characters. Example:
var s ="价格4500元"; var num= s.replace(/[^0-9]/ig,""); alert(num);//4500
If you encounter characters with numbers:
var s ="价格4500元,等级:2"; var num = s.replace(/[^0-9]/ig,""); alert(num);//45002
The result will be 45002.
The value obtained when js reads a text box or other form data is of string type, for example, two text boxes a and b. If the value of a is 11, and the value of b is 9, then a.value should be smaller than b.value, because they are both in the form of strings. I found articles on converting js strings to numbers on the Internet. This is a relatively complete method. There are three main methods
Conversion function, forced type conversion, weak type conversion using js variables.
1. Conversion function:
js provides two conversion functions: parseInt() and parseFloat(). The former converts the value to an integer, and the latter converts the value to a floating point number. Only by calling these methods on the String type can these two functions run correctly; for other types, NaN (Not a Number) is returned.
Some examples are as follows:
Copy code The code is as follows:
parseInt("1234blue"); //returns 1234 parseInt("0xA"); //returns 10 parseInt("22.5"); //returns 22 parseInt("blue"); //returns NaN
The parseInt() method also has a base mode, which can convert binary, octal, hexadecimal or any other Convert hexadecimal string to integer. The base is specified by the second parameter of the parseInt() method, an example is as follows:
Copy code The code is as follows:
parseInt("AF", 16); //returns 175 parseInt("10", 2); //returns 2 parseInt("10", 8); //returns 8 parseInt("10", 10); //returns 10
If the decimal number contains leading 0, then it is best to use base 10, This way you won't accidentally get an octal value. For example:
Copy code The code is as follows:
parseInt("010"); //returns 8parseInt("010", 8); //returns 8parseInt("010", 10); //returns 10
The parseFloat() method is similar to the parseInt() method.
Another difference in using the parseFloat() method is that the string must represent a floating point number in decimal form, and parseFloat() has no base mode.
The following is an example of using the parseFloat() method:
Copy code The code is as follows:
parseFloat("1234blue"); //returns 1234.0 parseFloat("0xA"); //returns NaN parseFloat("22.5"); //returns 22.5 parseFloat("22.34.5"); //returns 22.34 parseFloat("0908"); //returns 908 parseFloat("blue"); //returns NaN
2. Forced type conversion
You can also use forced type Type casting deals with converting the type of a value. Use a cast to access a specific value, even if it is of another type.
The three forced type conversions available in ECMAScript are as follows:Boolean(value) - Convert the given value into Boolean type;
Number(value) - Convert the given value into a number (Can be an integer or floating point number);
String(value) - Convert the given value into a string.
Using one of these three functions to convert a value will create a new value that stores the value directly converted from the original value. This can have unintended consequences.
The Boolean() function returns true when the value to be converted is a string, non-zero number, or object with at least one character (this will be discussed in the next section). If the value is an empty string, the number 0, undefined, or null, it returns false.
You can use the following code snippet to test the forced type conversion of Boolean.
Copy code The code is as follows:
Boolean(""); //false – empty string Boolean("hi"); //true – non-empty string Boolean(100); //true – non-zero number Boolean(null); //false - null Boolean(0); //false - zero Boolean(new Object()); //true – object
Number()的强制类型转换与parseInt()和parseFloat()方法的处理方式相似,只是它转换的是整个值,而不是部分值。示例如下:
复制代码 代码如下:
用 法 结 果
Number(false) 0
Number(true) 1
Number(undefined) NaN
Number(null) 0
Number( "5.5 ") 5.5
Number( "56 ") 56
Number( "5.6.7 ") NaN
Number(new Object()) NaN
Number(100) 100
最后一种强制类型转换方法String()是最简单的,示例如下:
代码如下:
var s1 = String(null); //"null" var oNull = null; var s2 = oNull.toString(); //won't work, causes an error
3. 利用js变量弱类型转换
举个小例子,一看,就会明白了。
代码如下:
<script> var str= '012.345 '; var x = str-0; x = x*1; </script>
上例利用了js的弱类型的特点,只进行了算术运算,实现了字符串到数字的类型转换,不过这个方法还是不推荐的
方法主要有三种
转换函数、强制类型转换、利用js变量弱类型转换。
1. 转换函数
js提供了parseInt()和parseFloat()两个转换函数。前者把值转换成整数,后者把值转换成浮点数。只有对String类型调用这些方法,这两个函数才能正确运行;对其他类型返回的都是NaN(Not a Number)。
在判断字符串是否是数字值前,parseInt()和parseFloat()都会仔细分析该字符串。parseInt()方法首先查看位置0处的字符,判断它是否是个有效数字;如果不是,该方法将返回NaN,不再继续执行其他操作。但如果该字符是有效数字,该方法将查看位置1处的字符,进行同样的测试。这一过程将持续到发现非有效数字的字符为止,此时parseInt()将把该字符之前的字符串转换成数字。
例如,如果要把字符串 "1234blue "转换成整数,那么parseInt()将返回1234,因为当它检测到字符b时,就会停止检测过程。字符串中包含的数字字面量会被正确转换为数字,因此字符串 "0xA "会被正确转换为数字10。不过,字符串 "22.5 "将被转换成22,因为对于整数来说,小数点是无效字符。一些示例如下:
parseInt("1234blue"); //returns 1234
parseInt("0xA"); //returns 10
parseInt("22.5"); //returns 22
parseInt("blue"); //returns NaN
parseInt()方法还有基模式,可以把二进制、八进制、十六进制或其他任何进制的字符串转换成整数。基是由parseInt()方法的第二个参数指定的,所以要解析十六进制的值,需如下调用parseInt()方法:
parseInt("AF", 16); //returns 175
当然,对二进制、八进制,甚至十进制(默认模式),都可以这样调用parseInt()方法:
parseInt("10", 2); //returns 2
parseInt("10", 8); //returns 8
parseInt("10", 10); //returns 10
如果十进制数包含前导0,那么最好采用基数10,这样才不会意外地得到八进制的值。例如:
parseInt("010"); //returns 8
parseInt("010", 8); //returns 8
parseInt("010", 10); //returns 10
在这段代码中,两行代码都把字符串 "010 "解析成了一个数字。第一行代码把这个字符串看作八进制的值,解析它的方式与第二行代码(声明基数为8)相同。最后一行代码声明基数为10,所以iNum3最后等于10。
parseFloat()方法与parseInt()方法的处理方式相似,从位置0开始查看每个字符,直到找到第一个非有效的字符为止,然后把该字符之前的字符串转换成数字。不过,对于这个方法来说,第一个出现的小数点是有效字符。如果有两个小数点,第二个小数点将被看作无效的,parseFloat()方法会把这个小数点之前的字符串转换成数字。这意味着字符串 "22.34.5 "将被解析成22.34。
使用parseFloat()方法的另一不同之处在于,字符串必须以十进制形式表示浮点数,而不能用八进制形式或十六进制形式。该
方法会忽略前导0,所以八进制数0908将被解析为908。对于十六进制数0xA,该方法将返回NaN,因为在浮点数中,x不是有效字符。此外,parseFloat()也没有基模式。
下面是使用parseFloat()方法的示例:
parseFloat("1234blue"); //returns 1234.0
parseFloat("0xA"); //returns NaN
parseFloat("22.5"); //returns 22.5
parseFloat("22.34.5"); //returns 22.34
parseFloat("0908"); //returns 908
parseFloat("blue"); //returns NaN
2. 强制类型转换
还可使用强制类型转换(type casting)处理转换值的类型。使用强制类型转换可以访问特定的值,即使它是另一种类型的。
ECMAScript中可用的3种强制类型转换如下:
Boolean(value)——把给定的值转换成Boolean型;
Number(value)——把给定的值转换成数字(可以是整数或浮点数);
String(value)——把给定的值转换成字符串。
用这三个函数之一转换值,将创建一个新值,存放由原始值直接转换成的值。这会造成意想不到的后果。
当要转换的值是至少有一个字符的字符串、非0数字或对象(下一节将讨论这一点)时,Boolean()函数将返回true。如果该值是空字符串、数字0、undefined或null,它将返回false。
可以用下面的代码段测试Boolean型的强制类型转换。
Boolean(""); //false – empty string
Boolean("hi"); //true – non-empty string
Boolean(100); //true – non-zero number
Boolean(null); //false - null
Boolean(0); //false - zero
Boolean(new Object()); //true – object
Number()的强制类型转换与parseInt()和parseFloat()方法的处理方式相似,只是它转换的是整个值,而不是部分值。还记得吗,parseInt()和parseFloat()方法只转换第一个无效字符之前的字符串,因此 "4.5.6 "将被转换为 "4.5 "。用Number()进行强制类型转换, "4.5.6 "将返回NaN,因为整个字符串值不能转换成数字。如果字符串值能被完整地转换,Number()将判断是调用parseInt()方法还是调用parseFloat()方法。下表说明了对不同的值调用Number()方法会发生的情况:
用 法 结 果
Number(false) 0
Number(true) 1
Number(undefined) NaN
Number(null) 0
Number( "5.5 ") 5.5
Number( "56 ") 56
Number( "5.6.7 ") NaN
Number(new Object()) NaN
Number(100) 100
最后一种强制类型转换方法String()是最简单的,因为它可把任何值转换成字符串。要执行这种强制类型转换,只需要调用作为参数传递进来的值的toString()方法,即把1转换成 "1 ",把true转换成 "true ",把false转换成 "false ",依此类推。强制转换成字符串和调用toString()方法的唯一不同之处在于,对null或undefined值强制类型转换可以生成字符串而不引发错误:
var s1 = String(null); //"null"
var oNull = null;
var s2 = oNull.toString(); //won’t work, causes an error
3. 利用js变量弱类型转换
举个小例子,一看,就会明白了。
3f1c4e4b6b16bbbd69b2ee476dc4f83a
var str= '012.345 ';
var x = str-0;
x = x*1;
2cacc6d41bbb37262a98f745aa00fbf0
上例利用了js的弱类型的特点,只进行了算术运算,实现了字符串到数字的类型转换,不过这个方法还是不推荐的。
编程中经常会遇到取各个位数,总结了两个算法。
javascript代码
var num = 12345; //个位 var a = num%10; //十位 var b = num/10 % 10; //百位 var c = num/100 % 10; //...以此类推
1
2
3
4
5
6
7
8
javascript代码
var num = 123456; var numArr = num.split(''); //个位 var a = numArr[numArr.length-1]; //十位 var b = numArr[numArr.length-2]; //百位 var c = numArr[numArr.length-3]; //...以此类推
1
2
3
4
5
6
7
8
9
From the writing point of view, the second method is better than the first one. The performance has not been tested yet, and the learning skills are not good. If there are any mistakes, I hope experts can correct me!
Related recommendations:
js implementation of extracting numbers from strings_javascript skills
The above is the detailed content of Extract numbers from Js string. For more information, please follow other related articles on the PHP Chinese website!