連接(添加)字符串concat()函數
替換(修改/刪除)字符串replace()函數
檢索(搜索)字符串indexOf()、match()、search()函數
Math對象
數學常量屬性、絕對值Math.abs()、次方冪Math.pow()、隨機數Math.random()、四捨五入Math.round()、向上取整Math.ceil()和向下取整Math.floor()函數
Number對象
字符/串轉整數toString()函數、四捨五入爲指定小數的數值字符串toFixed()函數
1.1 String對象屬性
方法 |
描述 |
constructor |
對創建該對象的函數的引用 |
length |
字符串的長度 |
prototype |
允許您向對象添加屬性和方法 |
注意:如果只是定義了字符串對象,卻沒有創建字符串對象,那麼他是不具備向相對添加自定義的屬性和方法的。例如:
var Str = "What Are You doing?"; Str.num = 10; //undefined var StrObj = new String(); StrObj.num = 10; //10
1.2 String對象方法
方法 |
描述 |
連接字符串。 |
|
替換指定的或與正則表達式匹配的子串。 |
|
indexOf() |
檢索字符串。 |
檢索與正則表達式相匹配的值。 |
|
找到一個或多個與正則表達式匹配的值。 |
|
將字符串分割爲字符串數組。 |
|
字符串轉換爲小寫。 |
|
字符串轉換爲大寫。 |
1、concat()連接字符串
concat()方法將一個或多個字符串與原字符串連接合並,形成一個新的字符串並返回。連接字符串還可以通過操作符「+」來連接兩個字符串,稱爲字符串連接,他的作用和concat()函數是一樣的。
var str1 = "How",str2 = " Are",str3 = " You" console.log(str1.concat(str2,str3)); //How Are You var Str = str1 + str2 + str3; console.log(Str); //How Are You
2、match()方法查找字符串
這個方法是使用正則表達式模式對字符串進行搜索,並返回一個包含搜索結果的數組對象。該方法的語法格式爲:match(regExp)
如果沒有爲正則表達式設置全局標誌(g),match方法產生的結果與沒有設置全局標誌(g)的exec方法的結果完全相同。如果設置了全局標誌(g),match方法返回數組中包含所有完整的匹配結果,元素0~n依次是每個完整的匹配結果。
傳遞給match方法的參數是一個RegExp對象實例,及用表達式作爲match方法的參數去搜索字符串;而傳遞給exec方法的參數是一個String類型的對象實例,即用表達式對象去搜索作爲exec方法參數的字符串。
例如,在「Hello word」字符串中檢索不同的字串,代碼如下
var Str = "Hello word"; console.log("查找字符串word的結果爲:" + Str.match("word")); //word console.log("查找字符串WORD的結果爲:" + Str.match("WORD")); //null console.log("正則表達式匹配WORD的小寫結果爲:" + Str.match(/WORD/i)); //word
注意:檢索出來的結果是數組對象,並非字符串更不是字符串對象
var Str = "Hello word"; var Result = Str.match("word"); console.log("數組長度:"+Result.length); //1 console.log("字符串數組長度:"+Result[0].length); //4 Result[1] = "Ni Hao"; console.log("拼接的數組爲:"+Result); //word,Ni Hao
3、search()方法查找字符串
該方法返回使用表達式搜索時,第一個匹配到的字符串在整個被搜索字符串中的位置。該方法的語法格式爲:search(regExp)。
var Str = "Hello word"; console.log("查找字符串word的結果爲:" + Str.search("word")); //6 console.log("查找字符串WORD的結果爲:" + Str.search("WORD")); //-1 console.log("正則表達式匹配WORD的小寫結果爲:" + Str.search(/WORD/i)); //6
個人觀點:match和search方法都可以搜索字符串中是否存在指定的字符串。兩者不同的是,match方法如果檢索出,則返回一個對象,裏面存有指定的字符串對象,如果不存在則返回一個null值。search方法從字符串的0下標開始檢索,如果檢索出則返回第一個匹配到的初始下標,如果不存在則返回-1。
4、indexOf()f方法查找字符串
該方法可以查找子字符串在另外一個字符串裏第一次出現的位置,返回子字符串在目標字符串裏的索引,如果沒有找到就返回-1。還有個函數是lastIndexOf()它的作用和indexOf是一樣的,不同的地方是lastIndexOf()查找的是最後一次出現的位置。
var Str = "Hello word word"; console.log(Str.indexOf("word")); //6 console.log(Str.lastIndexOf("word")); //11
5、replace()方法
該方法將一些字符替換掉字符串中的指定字符,或替換掉匹配正則表達式的字符。該語法的格式爲:stringObjet.replace(regExp / subStr,replaceStr)
var Str = "What Are You doing?"; console.log(Str.replace(/doing/,"Laoye")); //What Are You Laoye? console.log(Str.replace("doing","Laoye")); //What Are You Laoye?
注意:如果想要替換掉字符串中所有指定的字符串,只需要在正則表達式中指定全局匹配模式即可。例如:Str.replace(/o/g,"xx")。
6、split()方法
該方法通過指定格式將一個字符串分割成子字符串數組。該方法的語法格式如下split([separator[,limit]])。
方法按照某種分割標誌字符將一個字符串拆分成若干個子字符時所產生的子字符串數組。separator是分割標誌符參數,可以是多個字符或者一個正則表達式,參數limit顯示返回元素的個數。
var Str = "Hello word"; console.log(Str.split("")); //H,e,l,l,o, ,w,o,r,d console.log(Str.split("",3)); //H,e,l console.log(Str.split(" ")); //Hello,word
個人觀點:沒有空格,表示的是每個字符都拆成一個子字符。而一個空格表示的,每個空格拆開的字符串都爲子字符串。
1、Math對象
Math對象提供了大量的數學常量和數學函數。Math對象不像Date和String那樣的對象,它沒有構造函數Math(),比如Math.sin()這樣的函數只是函數,不是某個對象的方法。所以,無需創建Math對象,直接使用「對象名.成員」的格式就可以訪問其屬性或方法。
1.1 Math對象的屬性
Math對象的屬性是數學中常用的常量。
1.2 Math對象的方法
Math對象的方法是數學中常用的函數
這裏一定要掌握比較常用的幾個函數,分別是abs、pow、random、round、ceil、floor,分別對應數值的絕對值、次方冪、隨機數、四捨五入、向上取整和向下取整,效果如:
console.log("-5的絕對值爲:" + Math.abs(-5)); //5 console.log("2的3次方爲:" + Math.pow(2,3)); //8 console.log("0~1隨機生成的浮點數爲:" + Math.random()); //0.350235... console.log("2.56向上取整爲:" + Math.ceil(2.56)); //3 console.log("2.56向下取整爲:" + Math.floor(2.56)); //2 console.log("2.5的四捨五入爲:" + Math.round(2.5)); //3
2、Number對象
Number對象是原始數值的包裝對象。它可以不與運算符new一起使用,而直接作爲轉化函數來使用。以這種方式調用Number()時,它會把自己的參數轉化成一個數字,然後返回轉換後的原始數值或者Nan。
console.log(Number("a")); //NaN -a並非數值字符 console.log(Number("10")); //10 console.log(typeof Number("10")); //number var num = new Number("10"); console.log(num); //10 console.log(typeof num); //object
2.1 Number對象的屬性
* MAX_VALUE屬性:返回Number對象的最大可能值
* MIN_VALUE屬性:返回Number對象的最小可能值
* NEGATIVE_INFINITY屬性:返回Number對象的負無窮大的值
* POSITIVE_INFINITY屬性:返回Number對象的正無窮大的值
* prototype屬性:可以爲對象添加屬性和方法
* constructor屬性:返回創建此對象的函數引用
2.2 Number對象的方法(主要介紹兩種)
* toString方法
該方法可以把Number對象轉換成一個字符串,並返回結果。該方法的語法格式爲:NumberObject.toString(radix)
注意:radix是可選項,默認爲基數爲10,即表示爲十進制。它的值使用2~36之間的整數,如果參數是10以外的其他值,則ECMAScript標準允許返回任何值。
var num = new Number(120); console.log(typeof num); //object console.log(typeof num.toString()); //string console.log(num.toString(2)); //1111000 console.log(num.toString(10)); //120 console.log(num.toString(16)); //78 console.log(num.toString(36)); //3c
* toFixed方法
該方法將Number對象四捨五入爲指定小數位數的數字,然後轉換成字符串。該語法格式如下:NumberObject.toFixed(num)。
注意:num是必選項,默認爲0。規定小數的位數,是0~20之間的值。
var num = new Number(12.34567); console.log(num.toFixed(0)); //12 console.log(num.toFixed(1)); //12.3 console.log(num.toFixed(2)); //12.35 console.log(num.toFixed(3)); //12.346