日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

Javascript中操作String字符串的33種方法,你都知道嗎?

 

1、anchor

創建一個錨點。

document.write('hello'.anchor('anchor'))
// 輸出:<a name="anchor">hello</a>

2、big

創建一個加粗的文本 big 標簽包裹。

document.write('hello'.big())
// 輸出:<big>hello</big>

3、blink

創建一個閃爍的文本(目前只有 Firefox 和 Opera 支持)。

document.write('hello'.blink())
// 輸出:<blink>hello</blink>

4、bold

創建一個加粗的文本 b 標簽包裹。

document.write('hello'.bold())
// 輸出:<b>hello</b>

5、charAt

返回指定索引處的字符。

'hello'.charAt(1)
// 輸出:e

6、charCodeAt

返回指定位置的字符的Unicode值。

'hello'.charCodeAt(1)
// 輸出:101

7、concat

返回包含兩個或多個字符串的串聯的字符串。

var str1 = "world"
document.write('hello'.concat(str1));
// 輸出:hello world

8、fixed

創建一個加粗的文本 tt 標簽包裹。

document.write('hello'.fixed())
// 輸出:<tt>hello</tt>

9、fontsize

創建一個指定文本大小 font 標簽包裹。參數從 1 到 7 的數字

document.write('hello'.fontsize("5"))
// 輸出:<font size="5">hello</font>

10、fontcolor

創建一個指定文本顏色 font 標簽包裹。

document.write('hello'.fontcolor("red"))

11、indexOf

返回第一次出現的子字符串的位置。

第二個參數可選的,搜索String對象的索引,如果省略,則搜索從字符串的開頭開始。

該方法返回String對象的子字符串的開始。如果未找到子字符串,則返回-1。

如果第二個參數為負,則被視為零。如果它大于最高索引,則將其視為最高索引。

'hello'.indexOf('e')
// 輸出:1
'hello'.indexOf('e',2)
// 輸出:-1

12、italics

創建一個斜體的文本 i 標簽包裹。

document.write('hello'.italics())
// 輸出:<i>hello</i>

13、lastIndexOf

返回字符串中子字符串的最后一次出現。

從字符串的最后一個字符開始執行搜索

'hello'.lastIndexOf('l')
// 輸出:3
'hello'.lastIndexOf('l',2)
// 輸出:2
'hello'.lastIndexOf('l',0)
// 輸出:-1

14、link

創建一個帶鏈接的文本 a 標簽包裹。

'hello'.link('http://www.baidu.com')
// 輸出: <a href="http://www.baidu.com">hello</a>

15、localeCompare

比較兩個字符串在當前語言環境中是否等效

如果str1在str2之前排序,則返回-1。

如果str1在str2之后排序,則返回1。

返回值為0表示兩個字符串相等。

var str1 = "def";
 var str2 = "abc"
console.log(str1.localeCompare(str2));
 // 輸出: 1
 var str3 = "ghi";
 console.log(str1.localeCompare(str3));
 // 輸出: -1
 var str4 = "def";
 cstr1.localeCompare(str4));
 // 輸出: 0

16、match

將字符串與正則表達式匹配,并返回包含該搜索結果的數組。

var src = "azcafAJAC";
 var re = /[a-c]/g;
console.log(src.match(re));
 // 輸出: ["a", "c", "a"]

17、replace

使用正則表達式或搜索字符串替換字符串中的文本。

var s = "the batter hit the ball with the bat";
 var re = /the/g;
 var result = s.replace(re, "a");
console.log(result);
// 輸出: a batter hit a ball with a bat

18、search

在正則表達式搜索中查找第一個子字符串匹配項。

var src = "is but a Dream within a dream";
 var re = /dream/;
 var pos = src.search(re);
 console.log(pos);
// 輸出: 24

19、slice

返回字符串的一部分。

var str1 = "all good boys do fine";

 var slice1 = str1.slice(0);
 var slice2 = str1.slice(0,-1);
 var slice3 = str1.slice(4);
 var slice4 = str1.slice(4, 8);

 document.write(slice1 + "<br/>");
 document.write(slice2 + "<br/>");
 document.write(slice3 + "<br/>");
 document.write(slice4);

 // 輸出:
 // all good boys do fine
 // all good boys do fin
 // good boys do fine
 // good

20、small

創建一個字體偏小的文本 small 標簽包裹。

document.write('hello'.small())
// 輸出:<small>hello</small>

21、split

使用指定的分隔符將字符串拆分為子字符串,然后將子字符串作為數組返回。

'hello'.split('e')
// 輸出: ["h", "llo"]

22、strike

創建一個帶刪除線的文本 strike 標簽包裹。

document.write('hello'.strike())
// 輸出:<strike>hello</strike>

23、sub

把字符串顯示為下標 sub標簽包裹。

document.write('hello'.sub())
c

24、substr

獲取一個從指定位置開始并具有指定長度的子字符串。

var s = "The quick brown fox jumps over the lazy dog.";
 var ss = s.substr(10, 5);
console.log(ss);
// 輸出:// 輸出:<sub>hello</sub>

25、substring

返回String對象中指定位置的子字符串。

var s = "The quick brown fox jumps over the lazy dog.";
 var ss = s.substring(10, 15);
console.log(ss);
// 輸出:brown

26、sup

把字符串顯示為上標 sup標簽包裹。

document.write('hello'.sup())
// 輸出:<sup>hello</sup>

27、toLocaleLowerCase

根據當前語言環境,將所有字母字符轉換為小寫。

console.log("HELLO".toLocaleLowerCase()); 

28、toLocaleUpperCase

根據當前語言環境,將所有字母字符轉換為大寫。

var hello = "hello";
var foobar = hello.toLocaleUpperCase();
console.log(foobar );
// 輸出:HELLO

29、toLowerCase

將字符串中的所有字母字符轉換為小寫。

var hello = "HELLO";
var foobar = hello.toLowerCase();
console.log(foobar);
// 輸出:hello

30、toUpperCase

將字符串中的所有字母字符轉換為大寫。

var hello = "hello";
var foobar = hello.toUpperCase();
console.log(foobar );
// 輸出:HELLO

31、toString

返回字符串。

var string = "this is a test";
 var strStr = string.toString();
 document.write(strStr);
 // 輸出: this is a test

32、trim

從字符串中刪除開頭和結尾的空格和行終止符。

var message = "    abc def     rn  ";
console.log(message.trim())
// 輸出: abc def

33、valueOf

返回字符串的值。

var str = "every good boy does fine";
var strStr = str.valueOf();

if (str === strStr)
document.write("same");
else
document.write("different");

// var str = "every good boy does fine";
var strStr = str.valueOf();

if (str === strStr)
document.write("same");
else
document.write("different");

// Output:
// same:
// same

如有錯誤和遺漏,歡迎指正,謝謝。

分享到:
標簽:Javascript String
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定