Convert string to number
1parseInt() function
parseInt() 函数从string的开始解析,返回一个整数 parseInt('123') : 返回 123; parseInt('1234xxx') : 返回 1234; parseInt('123.456') : 返回 123; parseInt('1 2 3') : 返回 1;字符串的情况,自会返回第一个数 parseInt('bb cc 12') : 返回 NaN;字符串第一个不是数,返回nan parseInt('123' 321) : 返回 321; parseInt("AF", 16); 返回 175;会自动把二进制十六进制八进制的转化成数字 如果解析不到数字,则将返回一个NaN的值,可以用isNaN()函数来检测; parseFloat()只会返回小数
2 Add " before +”
var n =+"123":返回 123
3 characters plus “*”
var n ="123"*1:返回 123
4 string array splitting added
var arr = str.split(",") 一个字符串组,用,都分割成多个 b = a.join("-");-是指定的分割符,可以随意,将数组转化成一个字符串
5 Adding strings and numbers returns a string
console.log(12+"12"):返回"1212" console.log('12' + '34')返回'1234' console.log('12' + 34 ) 返回'1234' console.log(12 + '34'')返回1234' console.log(12 + 34 ) 返回46
6 When adding strings and multiple numbers, use Expand the brackets
("<img src='images/" + 2 + 1 + ".jpg'>") 返回("<img src='images/" + 21 + ".jpg'>") 如果想转成3 ("<img src='images/" + (2+1)+ ".jpg'>") 返回("<img src='images/" + 3 + ".jpg'>") 如果前面是数字相加,则可以先把数字加起来 var a = 10 + 20 + 'abc' + 'cd'; 返回a的值为:30abccd
7eval()If you want to perform string operations
eval("x=10;y=20;document.write(x*y)") 返回200 document.write(eval("2+2")) 返回4 var x=10 document.write(eval(x+17) 返回27
Number to string
1tostring() n = 100 x = n.toString() => "100"或是(100)toString() //tostring(2/16/8)还可以实现进制的转化 2数字+任意字符串“” var n = 1234; var nn = 1234+""
About string
Judgement Whether the string contains a string that contains the return subscript and does not contain the string that returns -1
var i = str.indexOf("x")
The last subscript
var i = str .lastIndexOf("")
Convert to uppercase and lowercase
str.toUpperCase() 转大写 str.tolowerCase() 转小写
Intercept string string.substr ("Start from", "Intercept a few")
str.substr(0,5) <body>
The above is the detailed content of Problems with converting numbers and strings to each other in JS. For more information, please follow other related articles on the PHP Chinese website!