var str="Hello world!"
document.write("<br/>"+left(str,3));
document.write("<br/>"+right(str,4));
document.write("<br/>"+leftField(str,"e"));
document.write("<br/>"+rightField(str,"e"));
Output:
-----------------------------
Hel
rld!
H
llo world!
rld!
H
llo world!
函數 source code 如下:
-----------------------------
function trim(stringToTrim)
{
if(stringToTrim.length>0){
return stringToTrim.replace(/^\s+|\s+$/g,"");
}
}
function left(str, num)
{
return str.substring(0,num)
}
function right(str, num)
{
return str.substring(str.length-num,str.length)
}
function leftField(str,delimit)
{
var returnValue=str;
var indexNumber=str.indexOf(delimit);
if(indexNumber>=0){
returnValue = str.substring(0,str.indexOf(delimit))
}
return returnValue;
}
function rightField(str,delimit)
{
var returnValue=str;
var indexNumber=str.indexOf(delimit);
if(indexNumber>=0){
returnValue = str.substring(indexNumber+delimit.length,str.length)
}
return returnValue;
}
說明:
leftField + rightField, 取文字裡特定分隔符號的左邊, 和右邊.
說明:
leftField + rightField, 取文字裡特定分隔符號的左邊, 和右邊.
沒有留言:
張貼留言