原生JavaScript---字符串[2]
字符串的内容太多了,继续聊吧!
1.把字符串内容全部转化为小写
[javascript]
var txtString1="PrimitiveJavaScript is my JavaScript Special column.";
document.write("结果:"+txtString1.toLowerCase());//toLowerCase()方法用来把字符串转化为小写
var txtString1="PrimitiveJavaScript is my JavaScript Special column.";
document.write("结果:"+txtString1.toLowerCase());//toLowerCase()方法用来把字符串转化为小写
2.把字符串内容全部转化为大写
[javascript]
var txtString1="PrimitiveJavaScript is my JavaScript Special column.";
document.write("结果:"+txtString1.toUpperCase());//toUpperCase()方法用来把字符串转化为小
var txtString1="PrimitiveJavaScript is my JavaScript Special column.";
document.write("结果:"+txtString1.toUpperCase());//toUpperCase()方法用来把字符串转化为小
3.字符串连接字符串
“+”操作符在JavaScript中既可以用于算术运算,也可用于字符串间的连接。
[javascript]
var txtString1="PrimitiveJavaScript is my JavaScript Special column.";
var txtString2="Do you like it?";
var txtString3=txtString1+txtString2;//注意"+"
document.write("结果:"+txtString3);
var txtString1="PrimitiveJavaScript is my JavaScript Special column.";
var txtString2="Do you like it?";
var txtString3=txtString1+txtString2;//注意"+"
document.write("结果:"+txtString3);
当然,你也可以用内建方法concat()来连接两个字符串,不过这个方法不常用。
[javascript]
var txtString1="PrimitiveJavaScript is my JavaScript Special column.";
var txtString2="Do you like it?";
var txtString3=txtString1.concat(txtString2);
document.write("结果:"+txtString3);
var txtString1="PrimitiveJavaScript is my JavaScript Special column.";
var txtString2="Do you like it?";
var txtString3=txtString1.concat(txtString2);
document.write("结果:"+txtString3);
4.字符串连接numeric
在JavaScript中,numeric类型包括整型(int)和浮点型(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("<br/>");
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("<br/>");
document.write("result2:"+result2);
结果:
看到这你或许有疑问了,那个浮点型数明明是100.210,怎么结果是100.21?
JavaScript 引擎检测到“+”操作符两边一个为字符串,一个为非字符串时,它会把那个非字符串转换为字符串,然后
再与那个字符串连接;在转换浮点型变量时,如果检测到小数点以后最右面为0时会把0替换为"",这就是为什么会出
现上面的结果。
补充:web前端 , JavaScript ,