字串物件(js)

2022-07-02 19:42:09 字數 4373 閱讀 7332

//

1、字面量建立(推薦)(成對的單雙引號引著的就是字串)

var str1 = '花自飄零水自流';

console.log(str1);

console.log(

typeof str1); //

string

//2、函式建立

var str2 = string('一種相思,兩處閒愁');

console.log(str2);

console.log(

typeof str2); //

string

//3、建構函式建立

var str3 = new string('我不一樣');

console.log(str3);

console.log(

typeof str3); //

object

var str = 'hello';

console.log(str.charat(1)); //

econsole.log(str[1]); //

e ie7及以下不支援

var str = '0123456789';

console.log(str.charcodeat(0)); //

0--48

console.log(str.charcodeat(str.length - 1)); //

9--57

​var str2 = 'abcd...z';

console.log(str2.charcodeat(0)); //

a--97

console.log(str2.charcodeat(str2.length - 1)); //

z--122

​var str3 = 'abcd...z';

console.log(str3.charcodeat(0)); //

a--65

console.log(str3.charcodeat(str3.length - 1)); //

z--90​//

------------------------------------

console.log(('小').charcodeat(0)); //

23567

console.log(('姐').charcodeat(0)); //

22992

console.log(('姐').charcodeat(0)); //

22992

​console.log(string.fromcharcode(23567, 22992, 22992)); //

小姐姐

主要作用:檢查這個字元,在這個字串中是否存在

var str = 'abcdaaefg';

​console.log(str.indexof('a')); //

0console.log(str.indexof('cd')); //

2console.log(str.indexof('a', 1)); //

4console.log(str.indexof('a', 6)); //-1​

//---------------------------

console.log(str.lastindexof('a')); //

5console.log(str.lastindexof('a', 3)); //

0

案例:字串去重

var str = 'abcabc'; //

封裝乙個方法,用於去掉重複的字元,返回abc

console.log(fn(str)); //

abc​

​function

fn(str)

}return

newstr;

}

var str = 'abcdefg';

//

字串.slice(起始下標, 結束下標); 標準方法

console.log(str.slice()); //

abcdefg 沒有引數,返回全部

console.log(str.slice(2)); //

cdefg 有乙個引數,從第乙個引數處起,一直到最後

console.log(str.slice(2, 6)); //

cdef 有兩個引數,從第乙個引數處起,到第二個引數處止,不包括第二個引數

console.log(str.slice(6, 2)); //

'' 第二個引數比第乙個小(非負數),返回空字串

console.log(str.slice(2, -2)); //

cde 負數同長度相加

//

字串.substring(起始下標, 結束下標);

console.log(str.substring()); //

abcdefg 沒有引數,返回全部

console.log(str.substring(2)); //

cdefg 有乙個引數,從第乙個引數處起,一直到最後

console.log(str.substring(2, 6)); //

cdef 有兩個引數,從第乙個引數處起,到第二個引數處止,不包括第二個引數

console.log(str.substring(6, 2)); //

cdef 第二個引數比第乙個小(非負數),則兩引數交換位置

console.log(str.substring(2, -2)); //

ab 負數當作0

//

字串.substr(起始下標, 擷取的個數);

console.log(str.substr()); //

abcdefg 沒有引數,返回全部

console.log(str.substr(2)); //

cdefg 有乙個引數,從第乙個引數處起,一直到最後

console.log(str.substr(2, 2)); //

cd

console.log(str.substr(2, -2)); //

'' 負數不擷取

案例:結合moment.js,通過身份證號獲取某個人的年齡
只能針對英文

var str = 'abcde';

​var s = str.touppercase(); //

轉大寫console.log(s); //

abcde

​console.log(s.tolowercase());

//abcde 轉小寫

var str = '2020-10-10';

var arr = str.split('-');

console.log(arr);

//[ "2020", "10", "10" ]

​console.log(str.split());

//[ "2020-10-10" ]

console.log(str.split('')); //

[ "2", "0", "2", "0", "-", "1", "0", "-", "1", "0" ]

console.log(str.split('0')); //

[ "2", "2", "-1", "-1", "" ]

var arr = ["2020", "10", "10"];

console.log(arr.join('-')); //

2020-10-10

console.log(arr.join()); //

2020,10,10

console.log(arr.join('')); //

20201010

var str = '王小二說:老王是隔壁的';

var s = str.replace('王', '李');

console.log(s);

//李小二說:老王是隔壁的

var str = '   小王說    ';

console.log(str);

console.log(str.trim());

js 字串減去字串 js 減去字串

redd.config裡面的時間戳和隨機字串是哪個簽名的時間戳和隨機字串 貼給你,你就明白拉。前台js獲取timestamp var d new date var timestamp date.parse d 當前日期減去1970年1月1號的值 後台獲取簽名,即signature,其中nocestr...

js物件 陣列轉換字串

物件轉換成字串需要使用tostring 方法。1 var a function 4 var b a.tostring 5 console.log a 函式式的function 6 console.log b 字串function 7 console.log typeof a function 8 c...

JS遍歷字串 陣列 物件

陣列 let array 22 fe4 83 39,22 for let i 0 i array.length i 0 22 1 fe4 2 83 3 39 4 22陣列 let array 22 fe4 83 39,22 for let i in array 0 22 1 fe4 2 83 3 3...