本文主要記錄一些字串中的方法
contact()------ 將兩個或多個字元的文字組合起來,返回乙個新的字串
var a = 'hello';var b = ',world'';
var c = a.contact(b); // hello,world
indexof()------ 返回字串中乙個子串第一處出現的索引(從左到右搜素)。如果沒有匹配項,返回-1
var a = 'hello';var index= a.indexof('e');
console.log(index); // 1
charat()------ 返回指定位置的字元
var a = 'hello';var getchar = a.charat(0);
console.log(getchar); // h
match()------ 檢查乙個字串匹配乙個正規表示式內容,如果沒有匹配到返回null
var re = new regexp(/^\w+$/);var is_alpha1 = a.match(re);
//is_alpha1 = "hello"
var is_alpha2 = b.match(re);
//is_alpha2 = null;
substring()------ 返回字串的乙個子串,傳入引數是起始位置和結束位置
var a = 'hello';var substring = a.substring(1);
console.log(substring); // 'ello'
var substring2 = a.substring(1,4);
console.log(substring2); // 'ello'
substring是閉合
//刪除最後一位字串 var astring = a.substring(0,a.length-1);
substr()------ 返回字串的乙個子串,傳入引數是起始位置和長度
var a = 'hello';var substring = a.substr(1,4);
console.log(substring); // 'ello'
replace()------ 用來查詢匹配乙個正規表示式的字串,然後使用新字串代替匹配的字串
var a = 'hello';var result = a.replace(正規表示式規則,'hi');
console.log(result); // 'hi'
slice()------ 提取字串的一部分,並返回乙個新的字串
var a = 'hello';var substring = a.slice(1);
console.log(substring);// 'ello'
var substrings = a.slice(1,4);
console.log(substrings); // 'ell' slice是[ ) 半閉半開
split()------ 通過將字串劃分成子串,將乙個字串做成乙個字串陣列
var a = 'hello';var arr = a.split("");
console.log(arr); // ["h", "e", "l", "l", "o"]
tolowercase() ------ 將整個字串轉成小寫字母
var a = 'hello';var lowerstring = a.tolowercase();
console.log(lowerstring);// 'hello'
touppercase() ------ 將整個字串轉成大寫字母
var a = 'hello';var upperstring = a.touppercase();
console.log(upperstring); // 'hello'
關於字串String
通過string類的實現原始碼可以獲知,string類是final類,通過byte陣列儲存字串。檢視substring concat和replace方法,發現都不是在原字串上進行操作,而是重新生成了乙個新字串物件,也就是操作完成後,最開始的字串並沒有被改變。記住 對string物件的任何改變都不影響...
關於字串 string類
1 字串 字串是儲存在記憶體的連續位元組中的一系列字元。儲存在連續位元組中的一系列字元意味著可以將字串儲存在char陣列中,其中每個字元都位於自己的陣列元素中。什麼時候char陣列是string型別?char dog 8 not a string char cat 8 a string 只有第二個陣...
String字串關於 的詳解
字串建立有兩種方式 方式一建立時,先解析字串常量 mystring 它會在常量池裡面的乙個字串常量列表中查詢,如果沒有找到,在堆裡面建立乙個包含字串行 mystring 的string物件s1,然後把這個string物件的字串行和引用作為名值對存放到常量池裡面的字元床常量列表中。如下圖所示 接下裡就...