以下是javaScript中的String使用方法:
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() 返回字串的長度 eg:var length = str.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;
}
轉載出處:http://blog.csdn.net/gabriel80/archive/2008/11/15/3304751.aspx