Home >Web Front-end >Front-end Q&A >How to convert string in javascript
JavaScript is one of the most commonly used programming languages today and is widely used in front-end development and node development. In these applications, string processing is a very common operation. In JavaScript, we can convert strings in many ways. This article will introduce some commonly used string conversion methods.
The toString() method is a basic way to convert an object into a string. For a string, we can use this method to convert it, because in JavaScript, strings appear in the form of objects. For example:
let num = 123; let str = num.toString(); // 字符串 "123"
When performing this conversion, we can also choose the base (i.e. base) to be used. For example:
let num = 10; let hexStr = num.toString(16); // 将10转换成16进制字符串 "a"
The parseInt() method is a method to convert a string into an integer (decimal). This method will first remove the non-numeric parts of the string, and then convert the purely numeric string to a number. For example:
let str = "123"; let num = parseInt(str); // 整数 123
When performing this conversion, we can also choose the base (i.e. base) to be used. For example:
let hexStr = "a"; let num = parseInt(hexStr, 16); // 将16进制字符串 "a" 转换为整数 10
When using the parseInt() method, you need to note that when trying to convert a string, this method will only convert the pure numeric part of the string and ignore leading and trailing spaces, but not Intervening spaces are ignored. For example:
let str = "123 "; let num = parseInt(str); // 整数 123
But if there are non-numeric characters in the middle, the conversion will stop, for example:
let str = "123 abc"; let num = parseInt(str); // 整数 123
parseFloat( ) method is a way to convert a string to a floating point number. Unlike parseInt(), this method will also convert the decimal part of the string into a floating point number. For example:
let str = "3.14"; let num = parseFloat(str); // 浮点数 3.14
When doing this conversion, you also need to note that when trying to convert a string, this method will only convert the pure numeric part of the string and ignore leading and trailing spaces, but not Intervening spaces are ignored. Same as parseInt(), conversion will stop if a non-numeric character appears in the middle.
let str = "3.14 abc"; let num = parseFloat(str); // 浮点数 3.14
Number() method can convert any JavaScript value to a number. In terms of string conversion, unlike parseInt() and parseFloat(), this method does not ignore the spaces in the middle, treats spaces as illegal input, and directly returns NaN (not a numeric value). For example:
let str = " 123 "; let num = Number(str); // 整数 123 let str1 = "3.14 abc"; let num1 = Number(str1); // NaN
It should be noted that when there are unconvertible characters in the string, the return values of different conversion methods are different. parseInt() will return the convertible part; parseFloat() will ignore letters and return the corresponding numbers; and Number() will directly return NaN.
Summary:
When converting strings, we can choose to use different methods according to specific needs. It should be noted that when trying to convert a string, some special circumstances should be considered, such as: processing of spaces, base conversion, unsuccessful conversion, etc.
The above is the detailed content of How to convert string in javascript. For more information, please follow other related articles on the PHP Chinese website!