SOURCE

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

var weclomeString="welcome";
document.write( "正常:"+weclomeString+"<br/>")
document.write( "获取字符串长度:"+ weclomeString.length+"<br/>")
document.write("小写:"+weclomeString.toLowerCase()+"<br/>")
document.write( "大写:"+ weclomeString.toUpperCase()+"<br/>")

document.write("<br/>-------------------<br/>");
document.write( weclomeString.charAt(2)+"<br/>");
document.write( weclomeString.charAt(6)+"<br/>");
document.write( weclomeString.substring(3)+"<br/>");
document.write( weclomeString.substring(0,2)+"<br/>");

document.write("<br/>-------------------<br/>");
var sentence="We have fun in our class. Do you have fun in your class?"
writeDocument(sentence);
writeDocument(sentence.replace("We","You"));
writeDocument(sentence.replace("class","programing class"));
writeDocument(sentence.replace(/class/g,"programing class"));

 document.write("<br/>-------------------<br/>");
var splistSentence="We have fun in our class";
writeDocument(splistSentence);
writeDocument(splistSentence.split(" "));
writeDocument(splistSentence.split(""));
writeDocument(weclomeString);
writeDocument(weclomeString.split(""));

document.write("<br/>-------------------<br/>");
writeDocument("//IndexOf-------------------------------");
writeDocument(sentence.indexOf("class"));
writeDocument(sentence.lastIndexOf("class"));
writeDocument(sentence.match("class"));
writeDocument(sentence.match(/class/g));
writeDocument(sentence.search(/class/g));

document.write("<br/>-------------------<br/>");
//实战题:统计某一个字符的个数
function countChar(stringValue,char){
    var charCount=0

    for(var i=0;i<stringValue.length;i++){
        if(stringValue.charAt(i).toLowerCase()==char){
            charCount++;
        }      
    }
    return charCount;
}
var stringValue="Can you can a can as a Canner can can a can";
writeDocument("There are"+countChar(stringValue,"c")+"c in sentence.");
writeDocument("There are"+countChar(stringValue,"a")+"a in sentence.");


 
document.write("<br/>-------------------<br/>");
//实战题:统计字符串中数字的个数
 function countNumber(stringtoCount) {
    var numCount = 0;
     for(var i=0;i<stringtoCount.length;i++){
         var char=stringtoCount.charAt(i);
         if(char!=" " && !isNaN(char)){
             numCount++;
         }
     }
     return numCount;
 }
  document.write(countNumber("2nksdhp395"));
console 命令行工具 X clear

                    
>
console