Native JavaScript --- string [2]

Source: Internet
Author: User
There are too many strings. Continue to talk! 1. Convert all string content to lowercase [javascript] vartxtString1 & quot; PrimitiveJavaScriptismyJavaScriptSpecialcolumn. & quot; document. write (& quot; Result: & quot; + txtString1.toL

There are too many strings. Continue to talk!

1. Convert all string content to lowercase letters


[Javascript]
Var txtString1 = "PrimitiveJavaScript is my JavaScript Special column .";
 
Document. write ("Result:" + txtString1.toLowerCase (); // The toLowerCase () method is used to convert the string to lowercase.

Var txtString1 = "PrimitiveJavaScript is my JavaScript Special column .";

Document. write ("Result:" + txtString1.toLowerCase (); // The toLowerCase () method is used to convert the string to lowercase.


2. Convert all string content to uppercase


[Javascript]
Var txtString1 = "PrimitiveJavaScript is my JavaScript Special column .";
 
Document. write ("Result:" + txtString1.toUpperCase (); // The toUpperCase () method is used to convert a string to a smaller value.

Var txtString1 = "PrimitiveJavaScript is my JavaScript Special column .";

Document. write ("Result:" + txtString1.toUpperCase (); // The toUpperCase () method is used to convert a string to a smaller value.

 

3. String connection string

The "+" operator can be used in both arithmetic operations and string connections in JavaScript.


[Javascript]
Var txtString1 = "PrimitiveJavaScript is my JavaScript Special column .";
Var txtString2 = "Do you like it? ";
Var txtString3 = txtString1 + txtString2; // note "+"
Document. write ("Result:" + txtString3 );

Var txtString1 = "PrimitiveJavaScript is my JavaScript Special column .";
Var txtString2 = "Do you like it? ";
Var txtString3 = txtString1 + txtString2; // note "+"
Document. write ("Result:" + txtString3 );

 

Of course, you can also use the built-in concat () method to connect two strings, but this method is not commonly used.


[Javascript]
Var txtString1 = "PrimitiveJavaScript is my JavaScript Special column .";
Var txtString2 = "Do you like it? ";
Var txtString3 = txtString1.concat (txtString2 );
Document. write ("Result:" + txtString3 );

Var txtString1 = "PrimitiveJavaScript is my JavaScript Special column .";
Var txtString2 = "Do you like it? ";
Var txtString3 = txtString1.concat (txtString2 );
Document. write ("Result:" + txtString3 );

 

4. String connection numeric

In JavaScript, the numeric type includes INTEGER (int) and floating point (float ).


[Javascript]
Var intNum = 100;
Var floatNum = 100.210;
Var txtString = "It is ";
Var result1 = txtString + intNum;
Var result2 = txtString + floatNum;
Document. write ("result1:" + result1 );
Document. write ("
");
Document. write ("result2:" + result2 );

Var intNum = 100;
Var floatNum = 100.210;
Var txtString = "It is ";
Var result1 = txtString + intNum;
Var result2 = txtString + floatNum;
Document. write ("result1:" + result1 );
Document. write ("
");
Document. write ("result2:" + result2 );
Result:

 



You may have doubts about this. The floating point number is obviously 100.210. How is the result 100.21?

The JavaScript Engine detects that either side of the "+" operator is a string, and the other is a non-string, it will convert the non-string to a string, and then

Connect with that string. If the rightmost side of the decimal point is 0 when the floating point variable is converted, the value 0 is replaced with "", which is why

The preceding result is displayed.

 

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.