如题,我的思路是把先转成二维数组,然后每个数组下标为0的转成大写,但是不知道怎么操作了

解决方案 »

  1.   


    function fun(str)  
    {     
        str = str.toLowerCase();     
        return str.replace(/\b(\w)/g, function(m){  
            return m.toUpperCase();  
        });    
    }  
    先全部转化成小写 ,然后用正则匹配首个字母,首个字母肯定是在边界上
      

  2.   

    放在notepad++ 按住alt键 选择首字母,右键设置成大写
      

  3.   

    css有个属性text-transform:capitalize;先把内容放到页面加上这个属性;然后通过innerText获取出来就是首字母大写
      

  4.   

    你这一串单词是连续不断的呢,比如:[helloworld]
    还是每个单词间有空格的呢?比如[hello world]如果是第二种情况的话,原理如下:
    1:先将字符串转为数组,用split()方法
    2:循环遍历数组
        2.1:取出数组中元素(字符串)的下标为0(即首字母)的子串.用charAt()方法
        2.2:将取出的首字母,转为大写,用toUpperCase()方法
        2.3:将大写字母替换掉小写字母,用replace()方法
        2.4:将替换完成的字符串重新放入数组中
    3:将新数组转为字符串,用join()方法
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
        var str = "hello world i love php and js";
        var arr = str.split(" ");
        for(var i=0;i<arr.length;i++){
            arr[i] = arr[i].replace(arr[i].charAt(0),arr[i].charAt(0).toUpperCase());
        }
        console.log(arr.join(" "));
        </script>
    </head>
    <body>
        
    </body>
    </html>
    我很过分的把代码全都缩在一行了..
      

  5.   

    text-transform:Capitalize ;这个css了解一下