数据结构--字符串

数字字符串转成数字

function strNumberToNumber (s) { let i = 0 let res = 0 while (i < s.length && s[i] >= '0' && s[i] <= '9') { // ans = ans * 10 + (s[i++] - '0'); let cur = +s[i] //字符串转整数 res = res * 10 + cur i++ } return res } const s = '4193' const result = strNumberToNumber(s) console.log('result', result) //4193

    推荐阅读