字串的一些用法總結

2021-09-10 23:18:11 字數 4347 閱讀 8111

1. 兩個用於訪問字串中特定字元 charat() 和 charcodeat()

兩者都只有乙個引數,不同的是前者返回字元,後者返回字元的字元編碼。 

var strin**alue = "hello world";

console.log(strin**alue.charat(1)); //"e"

var strin**alue = "hello world";

console.log(strin**alue.charcodeat(1)); //輸出"101"

2. 字串拼接方法 concat()

用於將一或多個字串拼接起來,返回拼接得到的新字串,實戰中還是更多的使用 『+』 操作符來拼接。

var strin**alue = "hello "; 

var result = strin**alue.concat("world");

console.log(result); //"hello world"

console.log(strin**alue); //"hello"

3. 字串建立方法 slice()、substr()、substring()

該三個方法都可以傳遞乙個或兩個引數,第乙個引數都是字串開始的位置,slice() 與 substring() 方法的第二個引數表示結束位置,返回第乙個引數到第二個引數之間的字串片段,substr() 的第二個引數表示返回字串的個數。三個方法第二個如果不給,則預設返回當前位置到最後的字串。舉例:

var strin**alue = "hello world"; 

console.log(strin**alue.slice(3)); //"lo world" 

console.log(strin**alue.substring(3)); //"lo world" 

console.log(strin**alue.substr(3)); //"lo world" 

console.log(strin**alue.slice(3, 7)); //"lo w" 

console.log(strin**alue.substring(3,7)); //"lo w" 

console.log(strin**alue.substr(3, 7)); //"lo worl"

如過傳入的值為負值,slice()方法會將傳入的負值與字串的長度相加,substr() 方法將負的第乙個引數加上字串的長度,而將負的第二個引數轉換為 0。最後,substring() 方法會把所有負值引數都轉換為 0。下面來看例子。

var strin**alue = " hello world "; 

var trimmedstrin**alue = strin**alue.trim();

console.log(strin**alue); //" hello world alert(trimmedstrin**alue); //"hello world"

4. 從字串中查詢子字串的方法 indexof() 和 lastindexof()

indexof()方法從字串的開頭向後搜尋子字串,而 lastindexof()方法是從字串的末尾向前搜尋子字串。舉例:

var strin**alue = "hello world"; 

console.log(strin**alue.indexof("o")); //4

console.log(strin**alue.lastindexof("o")); //7

這兩個方法都可以接收可選的第二個引數,表示從字串中的哪個位置開始搜尋。

var strin**alue = "hello world"; 

console.log(strin**alue.indexof("o", 6)); //7

console.log(strin**alue.lastindexof("o", 6)); //4

5. trim()方法

刪除字串前後的空格。舉例:

var strin**alue = " hello world ";

var trimmedstrin**alue = strin**alue.trim();

console.log(trimmedstrin**alue); //"hello world"

6. 字串大小寫轉換方法 tolowercase()、tolocalelowercase()、touppercase()和 tolocaleuppercase()

直接舉例:

var strin**alue = "hello world"; 

console.log(strin**alue.tolocaleuppercase()); //"hello world"

console.log(strin**alue.touppercase()); //"hello world"

console.log(strin**alue.tolocalelowercase()); //"hello world"

console.log(strin**alue.tolowercase()); //"hello world"

7. 字串的模式匹配方法  match()、search()、replace()

match()方法接受乙個引數要麼是乙個正規表示式,要麼是乙個 regexp 物件,與regexp的exec() 方法相同。

var text = "cat, bat, sat, fat"; 

var pattern = /.at/;

//與 pattern.exec(text)相同

var matches = text.match(pattern);

console.log(matches.index); //0

console.log(matches[0]); //"cat"

console.log(pattern.lastindex); //0

search()方法接受乙個引數要麼是乙個正規表示式,要麼是乙個 regexp 物件,如果匹配則返回第一次出現的位置,否則返回-1。

var text = "cat, bat, sat, fat"; 

var pos = text.search(/at/);

console.log(pos); //1

replace()方法第接受兩個引數可以是乙個 regexp 物件或者乙個字串(這個字串不會被轉換成正規表示式),第二個引數可以是乙個字串或者乙個函式。如果第乙個引數是字串,那麼只會替換第乙個子字串。要想替換所有子字串,唯一的辦法就是提供乙個正規表示式,而且要指定全域性(g)標誌。舉例:

var text = "cat, bat, sat, fat"; 

var result = text.replace("at", "ond");

console.log(result); //"cond, bat, sat, fat"

result = text.replace(/at/g, "ond");

console.log(result); //"cond, bond, sond, fond"

8. localecompare()

與操作字串有關的最後乙個方法是 localecompare(),這個方法比較兩個字串,並返回下列

值中的乙個:

下面是幾個例子:

var strin**alue = "yellow"; 

console.log(strin**alue.localecompare("brick")); //1

console.log(strin**alue.localecompare("yellow")); //0

console.log(strin**alue.localecompare("zoo")); //-1

本文結。

C 關於字串的一些常見用法

關於字串由於平時題目用的很多所以整理一下 string是c 標準庫的乙個重要的部分,主要用於字串處理。include using namespace std int main 看了這個 才知道,string也可以使用迭代器 include using namespace std int main i...

OC 不可變字串和可變字串的一些用法

字串 nsstring 不可變字串 1.初始化方法 初始化乙個空的字串 nsstring string nsstring alloc init nsstring string1 asds 字面量 nslog string1 根據字串初始化 nsstring string2 nsstring allo...

一些字串函式

1.right location,somenumber left location,somenumber select right location,2 from my contacts 返回location列中所有右數兩個字元 select left location,2 from my cont...