js 中常見的 字串操作

2021-08-21 21:56:28 字數 935 閱讀 8119

1、indexof(): 從左往右搜尋

var a = "hello,word";

// indexof() 檢測有某個字元 則返回這個字元的位置,沒有檢測出這個字元 則返回 -1

a.indexof('w');// -1

a.indexof('w');// 6

2、lastindexof(): 方法可返回乙個指定的字串值最後出現的位置,從後向前搜尋

lastindexof(searchvalue,fromindex)
searchvalue

必需。規定需檢索的字串值。

fromindex

可選的整數引數。規定在字串中開始檢索的位置。它的合法取值是 0 到 stringobject.length - 1。如省略該引數,則將從字串的最後乙個字元處開始檢索。

var a = "hello,word";

// 不指定從哪個位置開始搜尋 則預設從最後一位 搜尋

a.lastindexof("o");// 7

// 從 第六位 開始 從後往前 搜尋

a.lastindexof("o",5);// 4

3、charat(index):方法可返回指定 的 index 位置的字元

var a = "hello,word";

// charat 用於返回指定位置的 字元

a.charat("0");// h

4、charcodeat(): 返回指定位置的字元的 unicode 編碼。這個返回值是 0 - 65535 之間的整數

var a = "hello,word";

// 返回 指定位置的 字元的 unicode 編碼

a.charcodeat('0');// 104

python中常見的字串操作

如有字串mystr hello world and bjsxt yunshuxueyuan sxt beijing 以下是常見的操作 1 find 檢測 str 是否包含在 mystr中,如果是返回開始的索引值,否則返回 1 mystr.find str,start 0,end len mystr ...

python中常見字串操作小結

usr bin env python coding utf 8 print 建立字串 str1 learn python print str1,str1 0 str1 1 輸出整個字串,第乙個字元,最後乙個字元 print str1 6 切片 result learn result learn py...

ANSI C中常見的字串函式

strcpy 函式 把s2指向的字串 包括空字元 複製到s1指向的位置,返回值是s1,在呼叫strcpy函式的時候,s1可以不指向陣列的開始,例如 第乙個引數為s1 5,表示把s2指向的字串複製到s1 5指向的位置。char strcpy char s1,const char s2 strncpy ...