字符串、數字相互轉換


//--------------------字符串轉數字---------------------------
var s = "234";
//1、純數字轉換

//1 字符串在運算操作中會被當做數字類型來處理
s *= 1;

//2 字符前加“+”
console.log(+s);

//3 string的兩個轉換函數,只對string有效
parseInt(s); // 234
parseFloat(s); //234

//4 強制類型轉換
Number(s); // 234

 

//2、數字加字母等非數字轉換
var s = '234string';
parseInt(s); //234
parseFloat(s); //234.0

 


//--------------------數字轉換字符串---------------------------
var num=123123;
//1、toString()
console.log(num.toString());

//2、數字+任何字符串""
console.log(num+"");

 


// 關於字符串

//判斷是否包含某個字符串 包含返回下標 不包含返回-1
var i = str.indexOf("x")

//最后一次下標的字符串的字符串
var i = str .lastIndexOf("")

//轉化大小寫
str.toUpperCase() //轉大寫
str.tolowerCase()// 轉小寫

//截取字符串 字符串.substr("從哪開始","截取幾個")
str.substr(0,5)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM