# String 字符串對象
>[success] **String對象** 用于處理已有的字符串,字符串可以使用單引號或者雙引號。
## length屬性
length 屬性包含一個整數,用來指出 String 對象中的字符數。String 對象中的最后一個字符的索引為 length - 1。
~~~
<script>
var str = 'hello world';
console.log('str字符串長度為:'+str.length); // str字符串長度為: 11
</script>
~~~
## indexOf方法 在字符串中查找字符串
返回 String 對象內第一次出現子字符串的字符位置。
~~~
<script>
var str = 'hello world';
console.log(str.indexOf('world')); //執行結果為 6
</script>
~~~
## match方法 內容匹配
使用正則表達式模式對字符串執行查找,并將包含查找的結果作為數組返回。
~~~
<script>
var str = 'hello world';
console.log(str.match('world')); // ["world", index: 6, input: "hello world"]
</script>
~~~
## replace方法 替換內容
返回根據正則表達式進行文字替換后的字符串的復制。
~~~
<script>
var str = 'hello world';
console.log(str.replace('world','rose')); // hello rose
</script>
~~~
## toUpperCase方法 toLowerCase方法 字符串大小寫轉換
~~~
<script>
var str = 'hello world';
console.log(str.toUpperCase()); // HELLO WORLD
console.log(str.toLowerCase()); // hello world
</script>
~~~
## split方法 字符串轉成數組
將一個字符串分割為子字符串,然后將結果作為字符串數組返回。
~~~
<script>
var str = 'hello world';
console.log(str.split(' ')); // ["hello", "world"]
</script>
~~~
## search方法 字符串查找
返回與正則表達式查找內容匹配的第一個子字符串的位置。
~~~
<script>
var s = "The rain in Spain falls mainly in the plain.";
re = /falls/i; // 創建正則表達式模式。
r = s.search(re); // 查找字符串。
console.log(r); // 18
</script>
~~~
## slice方法 字符串截取
返回字符串的片段。
~~~
<script>
var str = 'hello world';
console.log(str.slice(6,8)); // wo
</script>
~~~
- 空白目錄
- JavaScript保留字
- JS事件
- JS面向對象
- JS內置對象
- 自定義對象
- String 字符串對象
- Date 日期時間對象
- Array 數組對象
- Math 對象
- DOM對象控制HTML
- getElementsByName
- getElementsByTagName
- getAttribute 獲取元素屬性
- setAttribute 設置元素屬性
- childNodes 訪問子節點
- parentNode 訪問父節點
- createElement 創建元素節點
- createTextNode 創建文本節點
- insertBefore 插入節點
- removeChild 刪除節點
- offsetHeight 網頁高度
- scrollHeight 網頁高度
- JS瀏覽器對象
- window對象
- 計時器
- history對象
- location對象
- screen對象
- navigator對象
- 彈出窗口
- cookies