字串查詢方法有6個,startswith(searchstring,position), endswith(searchstring,length), search(regexp), indexof(searchvalue,fromindex), lastindexof(searchvalue,fromindex), includes(searchstring,position)
startswith(searchstring,position),endswith(searchstring,length)
search(regexp)
indexof(searchvalue,fromindex),lastindexof(searchvalue,fromindex)
includes(searchstring,position)
從fromindex往左查,所以'hello world, hello world'.lastindexof('llo',3)的結果是2關於找索引值的函式,找到就是返回的找到的索引,沒找到就返回-1startswith()方法的第二個引數表示要查詢的位置,endswith()方法的第二個引數表示指定字串搜尋的長度。
1doctype html
>
2<
html
lang
="en"
>
3<
head
>
4<
meta
charset
="utf-8"
>
5<
title
>startswith()
title
>
6head
>
7<
body
>815
<
script
>
16var
str ="
to be, or not to be, that is the question.";
1718
console.log(str.startswith(
"to be
"));
//true
19console.log(str.startswith(
"not to be
"));
//false
20console.log(str.startswith(
"not to be",
10));
//true
21script
>
22body
>
23html
>
1doctype html
>
2<
html
lang
="en"
>
3<
head
>
4<
meta
charset
="utf-8"
>
5<
title
>endswith()
title
>
6head
>
7<
body
>824
<
script
>
25var
str ="
to be, or not to be, that is the question.";
2627
console.log( str.endswith(
"question.
") );
//true
28console.log( str.endswith(
"to be
") );
//false
29console.log( str.endswith(
"to be",
19) );
//true
30script
>
31body
>
32html
>
doctype html
>
<
html
lang
="en"
>
<
head
>
<
meta
charset
="utf-8"
>
<
title
>search()
title
>
head
>
<
body
>
<
script
>
varstr ="
hey jude";
varre =/
[a-z]/g;
varre2 =/
[.]/
g; console.log(str.search(re));
//returns 4, which is the index of the first capital letter "j"
console.log(str.search(re2));
//returns -1 cannot find '.' dot punctuation
script
>
body
>
html
>
doctype html
>
<
html
lang
="en"
>
<
head
>
<
meta
charset
="utf-8"
>
<
title
>indexof()
title
>
head
>
<
body
>
<
script
>
console.log(
'hello world
'.indexof(
'llo
'));
console.log(
'hello world
'.indexof(
'llo',
3));
script
>
body
>
html
>
doctype html
>
<
html
lang
="en"
>
<
head
>
<
meta
charset
="utf-8"
>
<
title
>lastindexof()
title
>
head
>
<
body
>
<
script
>
console.log(
'hello world, hello world
'.lastindexof(
'llo
'));
//15
console.log(
'hello world, hello world
'.lastindexof(
'llo',
3));//2
script
>
body
>
html
>
doctype html
>
<
html
lang
="en"
>
<
head
>
<
meta
charset
="utf-8"
>
<
title
>includes()
title
>
head
>
<
body
>
<
script
>
varstr ='
to be, or not to be, that is the question.';
console.log(str.includes(
'to be
'));
//true
console.log(str.includes(
'question
'));
//true
console.log(str.includes(
'nonexistent
'));
//false
console.log(str.includes(
'to be',
1));
//false
console.log(str.includes(
'to be
'));
//false
script
>
body
>
html
>
JS字串常用方法(自) 5 字串分割
字串分割方法是split separator separator 分割符 分隔符是字串,可以是多個字元,返回值是分割成的陣列 split separator 作用 將字串分割成陣列 引數 separator 分割符 分隔符是字串,可以是多個字元 返回值 分割成的陣列 console.log fry ...
JS字串常用方法(自) 3 字串重複
字串重複的函式是repeat 作用是對字串進行重複,引數是count 重複次數 返回值是成功操作的字串。repeat 作用 對字串進行重複 引數 count 重複次數 返回值 重複操作之後的字串 console.log abc repeat 2 abcabc 1 doctype html 2 htm...
JS字串常用方法(自) 1 字串去空格
字串去空格函式有trim 無引數,返回值去空格後的字串 trimstart 別名trimleft trimend 別名trimright trim 作用 trim 方法會從乙個字串的兩端刪除空白字元。在這個上下文中的空白字元是所有的空白字元 space,tab,no break space 等 以及...