javascript字串的操作一些方法總結

來源:互聯網
上載者:User

isFinite(123);//檢測是否為數字(這裡返回true)
isNaN(123);//檢測是不否為數字(這裡返回false)

'hello world'.indexOf('a',2);//尋找匹配字元的位置沒有則返回-1,注後面選擇性參數從某個位置開始,從0開始

 代碼如下 複製代碼

function countInstances(mainStr, subStr)
    {
        var count = 0;
        var offset = 0;
        do
        {
            offset = mainStr.indexOf(subStr, offset);
            if(offset != -1)
            {
                count++;
                offset += subStr.length;
            }
        }while(offset != -1)
        return count;
    }


'hello world'.lastIndexOf('a',2);//尋找匹配字元的位置從後往前沒有則返回-1,從0開始
'hello world'.substring(1,4);//提取字串中介於兩個指定下標之間的字元,從0開始
'hello world'.substr(1,4);//提取字串的某個部分,從0開始,第一個參數若-1則從最後一個開始截取,第二個參數為length,從0開始
'hello world'.slice(1,4);//提取字串的某個部分,從0開始,第一個參數若不能為負數,第二個參數為空白則截取開始以後的所有字元,從0開始
'hello world'.split(' ',3);//把字串分割為字串數組,參數2表示數組最大長度

'hello world'.match("world");//尋找字元是否在字串中有則返回該字串沒有測null
/world/.test('hello world');//是否匹配字元返回true & false
'hello world'.search(/world/i);//尋找字元的位置,i忽略大小寫
'hello world'.replace(/wor/,"row");//替換字串
'hello world'.length;//輸出字串長度

'hello world'.toUpperCase();//將字串轉化為大寫
'hello world'.toLowerCase();//將字串轉化為小寫
'hello world'.valueOf();//輸入原始碼,如var a=function(){var str="bbb";}輸入出本身

'hello world'.charAt(1);//返回指定位置的字元
'hello world'.charCodeAt(1);//返回指定位置的字元的Unicode編碼
String.fromCharCode(72,69,76,76,79);//返回指定位置的字元
'hello world'.concat('caonima')//連結字串
'hello world'.fontcolor('#999999')//指定的顏色來顯示字串如:<FONT color=#99999>hello world</FONT>
'hello world'.fontsize('caonima')//使用指定的尺寸來顯示字串。

'hello world'.link("http://www.classyuan.com");//把文字轉連結

IE6和IE8對JS字串操作的細微差別
本來以為JS代碼應該是沒有問題了,但是後來發現在IE6卻無法正常運行。下面是頁面導航的JS代碼:

 代碼如下 複製代碼

$(function(){    
            $("#tr1 div").bind("click", function(){//給所有在tr1裡面的DIV註冊一個OnClick事件  
                       var name = $(this).attr("id"); //擷取ID  
                    
                   if(name.indexOf('p')=="0") 
                   {      
                        for(var k=1;k<15;k++) 
                        {    
                            var cid="child"+k; 
 
                            if((name.length==7&&k==name.substring(6,7))||(name.length==8&&k==(name.substring(6,7)+name.substring(7,8)))) 
                            {                         
                                $("#"+cid+"").show();    
                            } 
                            else
                            {    
                                $("#"+cid+"").hide();   
                            } 
                        } 
                    }              
            }); 
        });   

$(function(){ 
            $("#tr1 div").bind("click", function(){//給所有在tr1裡面的DIV註冊一個OnClick事件
                       var name = $(this).attr("id"); //擷取ID
                 
                   if(name.indexOf('p')=="0")
                   {   
                        for(var k=1;k<15;k++)
                        { 
                            var cid="child"+k;

                            if((name.length==7&&k==name.substring(6,7))||(name.length==8&&k==(name.substring(6,7)+name.substring(7,8))))
                            {                      
                                $("#"+cid+"").show(); 
                            }
                            else
                            { 
                                $("#"+cid+"").hide();
                            }
                        }
                    }           
            });
        });  

剛開始的時候在IE8瀏覽器下,我直接擷取name的第一個字母,直接用數組的下標方法 name[0],可以擷取在Google瀏覽器運行可以,但是我給我同事用的時候他的是IE6說沒反映,我剛開始沒注意 還以為他是開玩笑,今天我用VS內建的瀏覽器瀏覽的時候發現真的有問題 ,鬱悶,以為是jquery不相容,找了半天都說jquery相容性好,沒有說相容性不好的,我一步一步調試終於發現在判斷name[0]==p這個問題上找到了,於是馬上換代碼name.substring(0,1),在進行測試,總算是沒有問題了,有時候看似很小的問題卻真是折磨人!提醒大家以後製作網頁的時候如果需要使用的jquery,還需多注意跨瀏覽器的問題,以及編碼的問題,在就是字元轉義

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.