strings.htmlの一般的なメソッド

<script type = "text / javascript">
//文字列の一般的なメソッド(元の文字列を変更せずに)<ES5
var str = 'hello world'

//1.charAt()インデックスに従って文字列の内容を検索して返します
/ /構文:string.charAt(検索するインデックス)
//戻り値:インデックスに対応する文字列(空の文字列はありません)
var res = str.charAt(4)
console.log(str)// hello world
console.log(res)// o


//2.charCodeAt()//
構文:string.charCodeAt(検索するインデックス)
//戻り値:インデックス位置(10進数)に対応する文字のエンコーディング
var res = str。 charCodeAt(0)
console.log(str)// hello world
console.log(res)// 104


//3.substring()インターセプト文字列
//構文:string.substring(開始位置インデックス、終了位置インデックス)パッケージの前
/ 後の戻り値:インターセプト位置の文字(パッケージの前と後ではない)
var res = str.substring(0,4)
console.log(str)// hello world
console.log(res)// hell


//4.substr()インターセプト文字列
//構文:string.aubstr(開始位置のインデックス、数)
//戻り値:位置の文字
var res = str.substr(2,5)
console.log(str)// hello world
console.log( res)//


llo w //5.concat()連結文字列
//構文:string.concat(連結する必要のある文字列)
//戻り値:連結文字列
var res = str.concat( 'you Good world '、' linux access ')// = str +' hello world '
console.log(str)// hello world
console.log(res)// hello world hello world linux access


//6.slice()文字列のインターセプトはsubstring()メソッドと同じです
//構文:string。Slice(開始位置インデックス、終了位置インデックス)パッケージの前後
//戻り値:インターセプトされた文字列
var res = str.slice(0,4 )
console.log(str)// hello world
console.log(res)// hell


//7.toUpperCase()文字列を大文字に
//構文:string.toUpperCase()
//戻り値:文字列の大文字のコンテンツ
var res = str.toUpperCase()
console.log(str)// hello world
console.log(res)// HELLO WORLD

var str3 = '123456'
var res = str3.toUpperCase()
console.log(str3)// 123456
console.log(res)/ / 123456

var str2 = 'HELLO WORLD'
//8.toLowerCase()文字列を小文字に
//構文:string.toLowerCase()
//戻り値:文字列の小文字content
var res = str2.toLowerCase()
console.log (str2)// HELLO WORLD
console.log(res)// hello world


var str4 = '1-2-3-4-5-6'
//9.split()与えられた条件に従って文字列を切り取り、文字列をセクションに分割し、配列に返します
//構文:string.split(カットに使用するもの)
//戻り値:配列を返す
var res = str4.split( '-')//空を渡します文字列の場合、1つの
console.log(str4)// 1-2-3-4-5-6
console.log(res)//(6)["1"、 "2" によってカットされます。、「3」、「4」、「5」、「6」]


var str5 = 'Hello、world、hello、world、hello、world'
//10.replace()は、文字列の内容を置き換えます(テキストクエリの場合)
//構文:string.replace(replace 、replace何)
var res = str5.replace( 'hello'、 'hello')
console.log(res)// hello、world、hello、world、hello、world
console.log(str5)// hello 、World、hello、world、hello、world(一度に1つしか置換できません)


//共通文字列メソッドES5

//1.indexOf()特定の文字のインデックス位置を検索
//最初の方法

//構文: String。IndeOf(文字フラグメント)
//戻り値:
//対応する文字フラグメントが見つかった場合、文字フラグメントの開始位置のインデックスが返されます
//対応する文字フラグメントが見つからない場合。次に-1を返します
var str6 = 'hello world'
var res = str6.indexOf( 'lo')
console.log(str6)// hello world
console.log(res)// 3

// 2番目の方法
//構文:文字列。IndeOf(文字フラグメント(1文字のみ、それ以外の場合は-1を返す)、そこから検索を開始するインデックス位置)
var res = str6.indexOf( 'l'、5)
console.log(str6)//こんにちは世界
console.log(res)// 9

</ script>

おすすめ

転載: www.cnblogs.com/d534/p/12711989.html