SOURCE

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

var welcomesString="Welcome";
document.write(welcomesString+"<br/>");
document.write(welcomesString.length+"<br/>");

document.write(welcomesString.toLowerCase()+"<br/>");
document.write(welcomesString.toUpperCase()+"<br/>");
//截取字符串
document.write(welcomesString.charAt(2)+"<br/>");
document.write(welcomesString.charAt(6)+"<br/>");
//截取字符串某一部分
document.write(welcomesString.substring(3)+"<br/>");
document.write(welcomesString.substring(0,2)+"<br/>");
//替换字符串

var sentence="we have fun in our class.Do you have fun in your class?"
writeDocument(sentence);
writeDocument(sentence.replace("class","program class"));
//正则表达式,字符串替代所有
writeDocument(sentence.replace(/class/g,"programming class"));
//分割字t符串
var splitSentence="we have fun in our class";
writeDocument(splitSentence);
writeDocument(splitSentence.split(" "));
//用空字符切开
writeDocument(welcomesString);
writeDocument(splitSentence.split(""));
document.write('<br/>------------------------------<br/>');
writeDocument(sentence.indexOf("class"));//首次出现
writeDocument(sentence.lastIndexOf("class"));//最后一次出现

document.write('<br/>-------函数一-----------------------<br/>');

var sentence1 = "Can you can a can as a Canner can can a can";
function countChar(sentence1, char) {
    var count = 0;  
    for(var i=0;i<sentence1.length;i++){

         var char = sentence1.charAt(i);
        //将每一个字符转换为大写,然后判断是否与“C”相等
          if (char.toUpperCase() == "C") {
             count += 1;
         }
    } 
    return count;
}
writeDocument("There are"+countChar(sentence1,"c")+"in sentence");

document.write('<br/>-----------函数二-------------------<br/>');


function countNumber(stringtoCount) {

    var numCount = 0;
    for(var i=0;i<sentence2.length;i++){
        var char=sentence2.charAt(i);
        //isNaN()对空格字符会转化为0,需要加个判断charAt(i)不能为空格
        if (char != " " && !isNaN(char)) {
            numCount++;
            }
        }
        return numCount;
}
sentence2="we have 3345 pen";
document.write("There are "+countNumber()+" number in the sentence2");
   

console 命令行工具 X clear

                    
>
console