Javascript String類的屬性及方法

來源:互聯網
上載者:User

String 類 Attribute and method
anchor()              建立一個<a>標籤的執行個體,將其name屬性設定為被傳遞給此方法的字串
big()                     將字串轉換為<big>標籤的一個執行個體
blink()                   將字串轉換為<blink>標籤的一個執行個體
bold()                   將字串轉換為<bold>標籤的一個執行個體
charAt()               返回傳遞給此方法的索引處的字元
concat()               串連被傳遞的兩個字串返回一個新字串。
fixed()                  將字元轉換<tt> 固定的pitch字型標籤的一個執行個體
fontcolor()            設定一個<font>標籤執行個體的color屬性
fontsize()             設定一個<font>標籤執行個體的size屬性
fromCharCode()   返回被傳遞給此方的ISO-Latin-1數字字元
indexOf()              返回被傳遞給此方法的字串在一個sting對象的執行個體中第一次出現的索引
italics()                  將字串轉換為<i>標籤的一個執行個體
lastIndexOf()         返回被傳遞給此方法的字串在一個sting對象的執行個體中最後一次出現處的索引
link()                      將字串轉換為<a>標籤的一個執行個體並且用被傳遞給此方法的URL設定的herf屬性
match()                 返回一個數組,此數組包含革於被傳遞給此方法的規則運算式而找到的匹配。
replace()               用被傳遞給此方法的規則運算式和替換字串對調,用它的strin執行個體執行一個尋找和替換。
search()               返回被傳遞給此方法的字串中找到的匹配的索引位置。如果沒有找到字元中距離,返回-1
Slice()                   返回被傳遞給此方法的開始和結束索引之間的字串。如果傳遞一個負值,索引以被傳遞的字串尾部作為參考。
Small()                  將字串換為<small>標籤的一個執行個體
Split()     返回被分割為段的字串,這個分割由被傳遞給此方法的字串和執行個體限制定義
Strike()                  將字串轉換為<strike>標籤的一個執行個體
Sub()     將字串轉換為<sub>標籤的一個執行個體
Substr()                返回從被索引位置開始包含要返回的一定個數的字元的字串。如果一個負值被傳遞,索引以被傳遞的字串的尾部作為參考
substring()            返回被傳遞的開始和結束索引之間的字元
sup()                     將字串轉換為<sup>標籤的一個執行個體
toLowerCase()     將字串中的所有字元轉換為小寫
toSource()             返回被傳遞的字元作為string對象的字串表示
toString()               將被傳遞的字元作為string類型返回
toUpperCase()      將字串中的所有字元轉換為大寫
length()                 返回字串的長度
prototype()           為程式提供一種向string對象執行個體添加屬性的能力

String 類的擴充:
String.prototype.ToCharArray=function(){
         return this.split("");
}
//倒序
String.prototype.Reverse = function(){
return this.split("").reverse().join("");
}
//是否包含指定字元
String.prototype.IsContains = function(str){
return (this.indexOf(str) > -1) ;
}
//判斷是否為空白
String.prototype.IsEmpty=function(){
return this=="";
}
//判斷是否是數字
String.prototype.IsNumeric = function(){
var tmpFloat=parseFloat(this);
if(isNaN(tmpFloat)) return false;
var tmpLen=this.length-tmpFloat.toString().length;
return tmpFloat+"0".GetSame(tmpLen)==this;
}
//判斷是否是整數
String.prototype.IsInt = function(){
if(this == "NaN") return false;
return this == parseInt(this).toString();
}
//合并多個空白為一個空白
String.prototype.resetBlank = function(){
return this.replace(/s+/g,"");
}
//除去左邊空白
String.prototype.LTrim = function(){
return this.replace(/^s+/g,"");
}
//除去右邊空白
String.prototype.RTrim = function(){
return this.replace(/s+$/g,"");
}
//除去兩邊空白
String.prototype.trim = function(){
return this.replace(/(^s+)|(s+$)/g,"");
}
//保留數字
String.prototype.getNum = function(){
    return this.replace(/[^d]/g,"");
}
// 保留字母
String.prototype.getEn = function(){
    return this.replace(/[^A-Za-z]/g,"");
}
// 保留中文
String.prototype.getCn = function(){
    return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g,"");
}
//擷取位元組長度
String.prototype.ByteLength=function()
{
return this.replace(/[^\x00-\xff]/g,"aa").length;
}
// 從左截取指定長度的字串
String.prototype.left = function(n){
    return this.slice(0,n);
}
// 從右截取指定長度的字串
String.prototype.right = function(n){
    return this.slice(this.length-n);
}
// HTML編碼
String.prototype.HTMLEncode = function(){
    var re = this;
    var q1 = [/x26/g,/x3C/g,/x3E/g,/x20/g];
    var q2 = ["&","<",">"," "];
    for(var i=0;i<q1.length;i++)
    re = re.replace(q1[i],q2[i]);
    return re;
}
//擷取Unicode
String.prototype.Unicode = function(){
var tmpArr=[];
for (var i=0; i<this.length; i++) tmpArr.push("&#" + this.charCodeAt(i) + ";");
return tmpArr.join("");
}
//指定位置插入字串
String.prototype.Insert=function(index,str){
return this.substring(0,index)+str+this.substr(index);
}
//複製字串
String.prototype.Copy=function(){
if(IE) window.clipboardData.setData("text",this.toString());
}
//追加格式化後的字串
String.prototype.AppendFormat=function(str){
var arg=arguments;
str=str.replace(/\{(\d+)\}/g,function(a,b,c)
{
   if(arg[parseInt(b)+1]) return arg[parseInt(b)+1];
   else return a;
});
return this+str;
}

建立一個String 對象,文法:new String(stringValue),這個調用會將參數轉換為字串,並作為一個String 對象。事實上任何一個字串常量都是一個String對象,可以將其直接作為對象來使用,這和使用new String()建立對象的區別是:typeof的傳回值不同,一個是“stirng",另一個是"object".

string.indexOf(searchString,position)----從position(可選)位置開始,搜尋字串中的第一個searchSting所出現的位置並返回。例如:"hello,jack".indexOf("hello")將返回0;
"abcabc".indexOf("a",1)將返回3;

string.lastIndexOf(searchString,position)--------從position(可選)位置開始,搜尋字串中的最後一個searchString所出現的位置並返回。如:"abcabc".lastIndexOf("b")將返回4

string.charAt(pos)-------返回字串中位置為pos的字元。例如:"abc".charAt(1)返回"b"

stirng.charCodeAt(pos)------返回字串中位置為pos的字元代碼。對於ASCII 字元,這將返回其ASCII代碼。例如:"abc".charCodeAt(0)返回97,表示字元"a"的ASCII碼。

string.slice(start,end)--------返回字串中起始位置為start,結束位置為end(不包括end)的子字串

string.split(separator,linmit)------將字串以separator作為分割符切割成多個子字串,並將他們作為一個數組返回。linmit(可選)表示數組的最大長度,超過的部分將被捨棄。separator分隔字元不被包含在任何子字串中,如果sepatator為空白字串,則返回字串中的字元序列組成的數組。如果split方法沒有任何參數,則返回僅包含字串本身,僅有一個元素的數組。

string.split(separator,linmit)-------例如:"a1,b1,c1".split(",")將返回["a1","b1","c1"];

"a,b,c".split(",",2)將返回["a","b"];

"a,b,c".split("")將返回["a",",","b",",","c"];

"ab,c".split()將返回["ab,c"]
string.substr(start,length)--------返回字串中起始位置為start,長度為length的子字串。例如:"abcdefg".substr(1,3)將返回"bcd";

string.substring(start,end)------返回字串中起始位置為start,結束位置為end(包括end)的子字串。這個方法和slice方法唯一不同體現在是否包含了end位置的字元。

替換和匹配字串

(1)replace(searchValue,replaceValue)方法
             該方法將字串中第一個出現的searchValue子字串替換為replaceValue,並返回新的字串。原有的字串不受影響。

例如:var str1="aaaa";

var str2=str1.replace("a","b");

alert(str2);//輸出"baaa"
alert(str1);//輸出"aaaa"

上面代碼中可以看出,使用replace函數僅能替換一個執行個體。如果要替換多個執行個體,則需要使用Regex,例如str.replace(/a/g,"b")能夠將"aaaa"替換為"bbbb".

(2)match(reExp)方法

從字串中搜尋出匹配regExpRegex的所有子字串,將他們作為一個數組返回。利用物件類型到布爾類型的轉換規則,還可以判斷一個字串是否匹配regExp表示的Regex。

例如:var strInput=prompt("請輸入一個數字:",0);

while(!strInput.match(/\d+/)){
          strInput=prompt("請輸入一個數字:",0);
}

(3)search(regExp)方法

從字串中搜尋出匹配regExpRegex的第一個子字串,返回其索引位置。例如:var str="aabcabcabc";

alert(str.search(/abc/g));//顯示“1”

(4)String 對象的大小寫轉換

var str="abc";

str.toLowerCase()//轉化小寫

str.toUpperCase()//轉化大寫

(5)String 對象的串連

var str="abc";

var str2=str.concact("def","ghi");

alert(str2);//將輸出"abcdefghi"

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.