1. 程式人生 > >JavaScript&jQuery.String對象.Number對象.Math對象

JavaScript&jQuery.String對象.Number對象.Math對象

指定位置 rim write 查看 body 包括 random AI string

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>‘); // 指定有效數個數,這裏指定為3 document.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對象