Introduction to String-digital methods in JavaScript

Source: Internet
Author: User
Tags numeric type casting

We know that the simplest way to spin a digital string is:

Force type conversions

To convert integers to strings, you must use the integer.tostring () static method or the String.valueof () static method to convert the string to an integer, and you must use Integer.valueof ().
As you can see, type conversions in JavaScript cannot be viewed as "coercion type conversions".


You can also use coercion type conversion (type casting) to handle the type of the converted value. You can use coercion type conversions to access a specific value, even if it is of another type.
The 3 mandatory type conversions available in ECMAScript are as follows:
Boolean (value)--converts a given value to a Boolean;
Number (value)-Converts a given value to a digit (can be an integer or floating point);
String (value)--converts the given value to a string.
Converting a value with one of these three functions creates a new value that holds 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 with at least one character, a number other than 0 digits, or an object (which is discussed in the next section). If the value is an empty string, number 0, undefined, or null, it returns FALSE.

You can use the following code snippet to test a Boolean type cast.

  code is as follows copy code

Boolean ("") ;  //false  –   empty   string
Boolean ("Hi");  //true   –   non-empty   string
Boolean (m);  //true  –   non-zero   number
Boolean (null);  //false  -   null
Boolean (0);   //false  -   Zero
Boolean (new   Object ());  //true & nbsp –   object

The force type conversion of number () is similar to the parseint () and parsefloat () methods, except that it converts the entire value, not the partial value. Remember, the parseint () and parsefloat () methods only convert the string before the first invalid character, so "4.5.6" is converted to "4.5". Force type conversion with number (), and "4.5.6" returns Nan, because the entire string value cannot be converted to numbers. If the string value can be completely converted, number () will determine whether to call the parseint () method or call the Parsefloat () method. The following table describes what happens when you call the number () method on a different value:

Usage results
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

The code is as follows:

The code is as follows Copy Code

<div style= "Background-color: #FFCC99; width:400px; margin:0px; padding:5px"
<script language= " JavaScript "type=" Text/javascript
Function Checkform () {
   
    var Age2;
    age2 = number (Document.frmTest.age.value) + 1;
    document.frmTest.age2.value = age2;
   /*
    var Salesforce_para;
    Salesforce_para = "name=" + document.frmTest.name.value;
    Salesforce_para + + + "&" + "age=" + document.frmTest.age.value;
    var targeturl = "test_js.php?" + Salesforce_para;
    window.location = TargetUrl;
    */
}
</script>

<form action= "test_js.php" method= "post" name= "Frmtest" >
<!--name: <input type= "text" name= "name" ><br/>-->
Ages: <input type= "text" name= "age" ><br/>
<input type= "button" name= "Submit" value= "Submit->> View Something" onclick= "Checkform ()" ><br
Something: <input type= "text" name= "Age2" >
</form>
</div>

Strings can only be additive (stitching)

string addition (stitching) is very common, but the string for subtraction, multiplication, division?

This seems to be difficult to define, in fact, there is no subtraction, multiplication, division operation of Strings.

But JavaScript is a dynamic language, and if you take these three actions with two strings, he will try to turn them into numbers and then do the appropriate action. For example:

The code is as follows Copy Code
Alert ("45"-"32"); 13alert ("5" * "6"); 30alert ("12"/"3"); 4

But this conversion operation is not much like parseint and parsefloat, and it is similar to number, for example:

The code is as follows Copy Code
Alert ("123a"-"2BC"); NaN
Alert (parseint ("123a")-parseint ("2BC")); 121
Alert (Number ("123a")-Number ("2BC"));

Nan that is, this conversion, like number, converts the impure numeric string to Nan, indicating that it is not a number.

And parseint, Parsefloat takes out the string to take out the front can be expressed as a number of parts, and ignore the following can not be expressed as a number of parts.

The most concise method of string digital

By using a string to perform an operation that it cannot do, you first try to turn the attribute into a number. We can convert a string to a number by adding a positive symbol to the string. Such as:

The code is as follows Copy Code

var num = + "45";
Alert (typeof num); There is an application of this method in Numberjquery, for example, how do we get a string to determine if he has only numbers? The jquery approach:

var string = "321"; This thing gets the string and what is at random
Alert (+string + "" = = string); True means a numeric string, otherwise there are other ways to take advantage of this feature, such as:

var num = "45"-0;
Alert (typeof num); Numbervar num = "45" * 1;
Alert (typeof num); Numbervar num = "45"/1;
Alert (typeof num); Number


For example, if you want to convert the string "1234blue" to an integer, then parseint () will return 1234, because when it detects character B, it stops the detection process. The literal number contained in the string is converted to numbers correctly, so the string "0xA" is correctly converted to Number 10. However, the string "22.5" will be converted to 22 because the decimal point is an invalid character for an integer. Some examples are as follows:

The code is as follows Copy Code

parseint ("1234blue"); Returns 1234
parseint ("0xA"); Returns 10
parseint ("22.5"); Returns 22
parseint ("Blue"); Returns NaN

The parseint () method also has a base pattern that converts binary, octal, hexadecimal, or any other string of strings into integers. The base is specified by the second parameter of the parseint () method, so to parse the hexadecimal value, you call the parseint () method as follows:

The code is as follows Copy Code
parseint ("AF", 16); Returns 175

Of course, for binary, octal, or even decimal (default mode), you can call the parseint () method:

The code is as follows Copy Code
parseint ("10", 2); Returns 2
parseint ("10", 8); Returns 8
parseint ("10", 10); Returns 10

If the decimal number contains a leading 0, it is best to use cardinality 10 so that you do not accidentally get the octal value. For example:

The code is as follows Copy Code
parseint ("010"); Returns 8
parseint ("010", 8); Returns 8
parseint ("010", 10); Returns 10

In this code, two lines of code parse the string "010" into a number. The first line of code regards this string as a octal value, parsing it in the same way as the second line of code (the Declaration cardinality is 8). The last line of code declares a cardinality of 10, so the iNum3 finally equals 10.


System issues

The 0x starts with 16, so both number and parseint convert the 16 binary, and so does the automatic conversion of the string:

The code is as follows Copy Code
Alert (parseint ("0x10")); 16
Alert (Number ("0x10")); 16
Alert (+ "0x10");

16 but parsefloat some awkward, he does not know 16, the result becomes this:

Alert (parsefloat ("0x10")); 0 More tragic is the beginning of the 0, we know that 0 can be used to represent 8, in number and string automatic conversion, 0 start as a decimal to get, such as:

The code is as follows Copy Code

Alert (Number ("010")); 10
Alert (+ "010"); 10 and parseint is very sad reminders, ECMAScript did not make this mandatory so the following situation occurred:

Alert (parseint ("010")); 8 in Firefox & Iealert (parseint ("010"));

Ten in Chrome? (?_?)? No wonder it's rare to see 8 in JavaScript, and if you want to make sure that 8 can only use parseint's second argument:

  code is as follows copy code

Alert ( parseint ("010", 8)); 8parseFloat continues to not recognize 8, so:

Alert (parsefloat ("010"));//10

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.