SOURCE

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

var welcomeString= "welcome";

writeDocument(welcomeString);
writeDocument(welcomeString.length+'</br>');//打印字符串长度

writeDocument("//case-----------------------------------------------");
writeDocument(welcomeString.toLowerCase());//小写
writeDocument(welcomeString.toUpperCase()+'</br>');//大写

writeDocument("//charAt-----------------------------------------------");
writeDocument(welcomeString.charAt(2));//charAt获取字符串里的元素
writeDocument(welcomeString.charAt(1)+'</br>');

writeDocument("//substring-----------------------------------------------");
writeDocument(welcomeString.substring(3));//截取字符串 从3开始到结束
writeDocument(welcomeString.substring(0, 2)+'</br>');//从0开始到2结束

writeDocument("//replace-----------------------------------------------");
var sentence = "We have fun in our class. Do you have fun in your class?"//定义字符串
writeDocument(sentence);//打印原本字符串
writeDocument(sentence.replace("class", "programming class"));//第一个为原字符串,第二个为替换字符串,替换第一个class
writeDocument(sentence.replace(/class/g, "programming class")+'</br>');//替换所有的class '/字符串/g'

writeDocument("//split-----------------------------------------------");
var splitSentence = "We have fun in our class";
writeDocument(splitSentence);
writeDocument(splitSentence.split(" "));//(" ")中间有空格,在空格处分割
writeDocument(welcomeString);
writeDocument(welcomeString.split(""));//("")中间无空格,按照每一个字符分割
writeDocument(sentence.split("")+'</br>');

writeDocument("//IndexOf-----------------------------------------------");
writeDocument(sentence.indexOf("class"));//首次出现 第19号字符
writeDocument(sentence.lastIndexOf("class"));//最后出现 第50号字符
writeDocument(sentence.match("class"));//找这个字符是否存在
writeDocument(sentence.match(/class/g));//找所有的class
writeDocument(sentence.search(/class/g));//只会返回第一个class的位置,只能使用正则表达式

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.");
console 命令行工具 X clear

                    
>
console