標籤:number split 操作 lower ast 分解 har one lan
String對象.Number對象.Math對象
String對象
String 對象的屬性和方法用於操作字串。
<!DOCTYPE html>
<html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><script>var str="I like javascript ";document.write(str);document.write(‘<br>‘);// 屬性長度document.write(str.length);document.write(‘<br>‘);// 轉大寫document.write(str.toUpperCase());document.write(‘<br>‘);// 轉小寫document.write(str.toLowerCase());document.write(‘<br>‘);// 返回指定位置的字元,不包括空document.write(str.charAt(5));document.write(‘<br>‘);// 返回字元的位置document.write(str.indexOf(‘a‘));document.write(‘<br>‘);// 返回字元最後一次出現的位置document.write(str.lastIndexOf(‘a‘));document.write(‘<br>‘);// 從字串中取指定範圍的字元,從開始,包括空格document.write(str.substring(0,4));document.write(‘<br>‘);// 將字串按分解規則分解成數組var value=str.split(" ");document.write(value[0]);document.write(‘<br>‘);// 去年字串開始和結尾的空格document.write(str.trim());document.write(‘<br>‘);// 查看和替換document.write(str.replace(‘javascript‘,‘C++‘));document.write(‘<br>‘);</script></body></html>Math對象
Number對象通常用於運算元字。
<!DOCTYPE html>
<html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><div id="info"></div><script>var numberOne=12.545678;// 四捨五入document.write(numberOne.toFixed());document.write(‘<br>‘);// 指定有效數個數,這裡指定為3document.write(numberOne.toPrecision(3));document.write(‘<br>‘);//以指數標記法返回該數值字串表示形式,可以帶參數指定小數位元document.write(numberOne.toExponential(3));document.write(‘<br>‘);</script></body></html>Math對象Math對象用於數學上的計算,如算平方,四捨五入,產生隨機數等等。<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><script> var numberTwo=23.344; // 常量PI document.write(Math.PI); document.write(‘<br>‘); // 產生隨機數,0~1,範圍從0到1以內,不包括1 document.write(Math.random()); document.write(‘<br>‘); // 將整數,取最近並大於它當前值的數 document.write(Math.ceil(numberTwo)); document.write(‘<br>‘); // 將整數,取最近並小於它當前值的數 document.write(Math.floor(numberTwo)); document.write(‘<br>‘); // 四捨五入 document.write(Math.round(numberTwo)); document.write(‘<br>‘);</script></body></html>
JavaScript&jQuery.String對象.Number對象.Math對象