SOURCE

function writeDocument(variable) {
    document.write(variable + "<br/>");
} 

var welcomeString="Welcome";

writeDocument(welcomeString);
//长度
writeDocument("-------------length-------------------------------");
writeDocument(welcomeString.length);

//大小写转换
writeDocument("-------------case-------------------------------");
writeDocument(welcomeString.toLowerCase());
writeDocument(welcomeString.toUpperCase());

//获取
writeDocument("-------------charAt-------------------------------");
writeDocument(welcomeString.charAt(2));
writeDocument(welcomeString.charAt(6));

//截取
writeDocument("-------------substring----------------------------");
writeDocument(welcomeString.substring(3));
writeDocument(welcomeString.substring(0,2));

//替换
writeDocument("-------------replace------------------------------");
var sentence="We have fun in our class. Do you have fun in your class?"
writeDocument(sentence);
//字符串名.replace(原字符串, 替换字符串)
writeDocument(sentence.replace("class","programming class"));
//字符串名.replace(正则表达式, 替换字符串)
writeDocument(sentence.replace(/class/g,"programming class"));

//分割字符串
writeDocument("-------------split---------------------------------");
//用空格作为分割符来分割
writeDocument(sentence.split(" "));
//分割每一个字符
writeDocument(welcomeString.split(""));

//检索字符串位置
writeDocument("-------------indexOf/lastIndexOf/match/search-------------------");
//字符串第一次出现的位置
writeDocument(sentence.indexOf("class"));
//字符串最后一次出现的位置
writeDocument(sentence.lastIndexOf("class"));
//第一个字符串
writeDocument(sentence.match("class"));
//所有目标字符串
writeDocument(sentence.match(/class/g));
//字符串第一次出现的位置
writeDocument(sentence.search(/class/g));

//综合
writeDocument("------------------------------------------------------");
//找出字符串“Can you can a can as a Canner can can a can”中找出所有c的个数,不区分大小写。
function countChar(stringToCount, char) {
    var charCount = 0;
    for(i=0;i<stringToCount.length;i++){
        //C-->c
        var lowchar=stringToCount.toLowerCase();
        //获取
        if(lowchar.charAt(i)==char){
            ++charCount;
        }
    }
    return charCount;
}
var stringToCount = "Can you can a can as a Canner can can a can";
writeDocument("There are " + countChar(stringToCount, "c") + " c in " + stringToCount); 

//统计字符串中数字的个数
writeDocument("-------------统计字符串中数字的个数-------------------");
function countNumber(stringtoCount) {
    var numCount = 0;

     for(i=0;i<stringtoCount.length;i++){
         //是数字
         if (!isNaN(stringtoCount.charAt(i)) && stringtoCount.charAt(i) != " "){
            ++numCount; 
         }
     }
     return numCount;
}
writeDocument("该字符串有" + countNumber("12hdg bwji13njh8h357d") + "个数字");

        
console 命令行工具 X clear

                    
>
console