JavaScript中将字符串转为整数的方法
在JavaScript中,有多种方法可以将字符串转换为整数。本文将介绍一些常用的方法,并提供详细的示例代码来演示这些方法的用法。
1. 使用parseInt()方法
parseInt()方法可以将字符串转换为整数。它接受两个参数,第一个参数是要转换的字符串,第二个参数是进制数(可选,默认为10)。
示例代码:
let str = "123";
let num = parseInt(str);
console.log(num); // 输出:123
Output:

let str = "10";
let num = parseInt(str, 2);
console.log(num); // 输出:2
Output:

2. 使用Number()方法
Number()方法可以将字符串转换为数字。如果字符串中包含非数字字符,转换结果为NaN。
示例代码:
let str = "456";
let num = Number(str);
console.log(num); // 输出:456
Output:

let str = "abc";
let num = Number(str);
console.log(num); // 输出:NaN
Output:

3. 使用parseFloat()方法
parseFloat()方法可以将字符串转换为浮点数。它会忽略字符串中的非数字字符,直到遇到第一个非数字字符为止。
示例代码:
let str = "3.14";
let num = parseFloat(str);
console.log(num); // 输出:3.14
Output:

let str = "3.14abc";
let num = parseFloat(str);
console.log(num); // 输出:3.14
Output:

4. 使用Math.floor()方法
Math.floor()方法可以将浮点数向下取整,从而将字符串转换为整数。
示例代码:
let str = "7.8";
let num = Math.floor(parseFloat(str));
console.log(num); // 输出:7
Output:

5. 使用+运算符
在JavaScript中,使用+运算符可以将字符串转换为数字。
示例代码:
let str = "100";
let num = +str;
console.log(num); // 输出:100
Output:

6. 使用Bitwise OR运算符
使用Bitwise OR运算符|可以将字符串转换为整数。
示例代码:
let str = "42";
let num = str | 0;
console.log(num); // 输出:42
Output:

7. 使用乘法运算符
使用乘法运算符*可以将字符串转换为整数。
示例代码:
let str = "50";
let num = str * 1;
console.log(num); // 输出:50
Output:

8. 使用运算符
使用双取反运算符~~可以将字符串转换为整数。
示例代码:
let str = "99";
let num = ~~str;
console.log(num); // 输出:99
Output:

9. 使用parseInt()和isNaN()方法结合
结合使用parseInt()和isNaN()方法可以将字符串转换为整数,并检查转换结果是否为有效数字。
示例代码:
let str = "123";
let num = parseInt(str);
if (!isNaN(num)) {
console.log(num); // 输出:123
} else {
console.log("转换失败");
}
Output:

10. 使用正则表达式
使用正则表达式可以提取字符串中的数字部分,并将其转换为整数。
示例代码:
let str = "deepinout.com 456";
let num = parseInt(str.match(/\d+/)[0]);
console.log(num); // 输出:456
Output:

11. 使用自定义函数
编写一个自定义函数,实现将字符串转换为整数的功能。
示例代码:
function stringToInt(str) {
let num = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] >= '0' && str[i] <= '9') {
num = num * 10 + (str[i] - '0');
}
}
return num;
}
let str = "789";
let num = stringToInt(str);
console.log(num); // 输出:789
Output:

12. 使用eval()函数
eval()函数可以执行字符串中的JavaScript代码,从而将字符串转换为整数。
示例代码:
let str = "123";
let num = eval(str);
console.log(num); // 输出:123
Output:

13. 使用parseInt()和parseFloat()结合
结合使用parseInt()和parseFloat()方法可以将字符串转换为整数。
示例代码:
let str = "3.14";
let num = parseInt(parseFloat(str));
console.log(num); // 输出:3
Output:

14. 使用Number()和parseInt()结合
结合使用Number()和parseInt()方法可以将字符串转换为整数。
示例代码:
let str = "42";
let num = Number(parseInt(str));
console.log(num); // 输出:42
Output:

15. 使用parseInt()和Math.floor()结合
结合使用parseInt()和Math.floor()方法可以将字符串转换为整数。
示例代码:
let str = "99.9";
let num = Math.floor(parseInt(str));
console.log(num); // 输出:99
Output:

16. 使用parseInt()和Bitwise OR运算符结合
结合使用parseInt()和Bitwise OR运算符|可以将字符串转换为整数。
示例代码:
let str = "88";
let num = parseInt(str) | 0;
console.log(num); // 输出:88
Output:

17. 使用parseInt()和乘法运算符结合
结合使用parseInt()和乘法运算符*可以将字符串转换为整数。
示例代码:
let str = "123";
let num = parseInt(str) * 1;
console.log(num); // 输出:123
Output:

18. 使用parseInt()和运算符结合
结合使用parseInt()和双取反运算符~~可以将字符串转换为整数。
示例代码:
let str = "456";
let num = ~~parseInt(str);
console.log(num); // 输出:456
Output:

19. 使用parseInt()和正则表达式结合
结合使用parseInt()和正则表达式可以将字符串转换为整数。
示例代码:
let str = "deepinout.com 789";
let num = parseInt(str.replace(/\D/g, ''));
console.log(num); // 输出:789
Output:

20. 使用parseInt()和自定义函数结合
结合使用parseInt()和自定义函数可以将字符串转换为整数。
示例代码:
function stringToInt(str) {
let num = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] >= '0' && str[i] <= '9') {
num = num * 10 + (str[i] - '0');
}
}
return num;
}
let str = "deepinout.com 123";
let num = parseInt(stringToInt(str));
console.log(num); // 输出:123
Output:

以上是一些常用的方法和技巧,用于将字符串转换为整数。根据具体的需求和场景,选择合适的方法来实现字符串到整数的转换。
极客笔记