当前位置:编程学习 > JS >>

javascript字符串的操作一些方法总结

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],可以获取在谷歌浏览器运行可以,但是我给我同事用的时候他的是IE6说没反映,我刚开始没注意 还以为他是开玩笑,今天我用VS自带的浏览器浏览的时候发现真的有问题 ,郁闷,以为是jquery不兼容,找了半天都说jquery兼容性好,没有说兼容性不好的,我一步一步调试终于发现在判断name[0]==p这个问题上找到了,于是马上换代码name.substring(0,1),在进行测试,总算是没有问题了,有时候看似很小的问题却真是折磨人!提醒大家以后制作网页的时候如果需要使用的jquery,还需多注意跨浏览器的问题,以及编码的问题,在就是字符转义

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,