function writeDocument(variable){
document.write(variable+"<br\>");
}
var welcomeString="Welcome";
writeDocument(welcomeString.charAt(3));//提取
writeDocument(welcomeString.charAt(6));
var sentence="we have fun in our class.Do you have fun in your class?";
writeDocument(sentence);//替换
writeDocument(sentence.replace("class","programming class"));// 替换第一个出现的
writeDocument(sentence.replace(/class/g,"programming class"));//替换所有出现的
writeDocument(sentence.split(" ")); //分割 用空格来分割字符串
writeDocument(sentence.split(""));
writeDocument(sentence.split("."));//没有空格的,所以可以用来分割字符串每一个字符
writeDocument(welcomeString.substring(3)); //截取
writeDocument(welcomeString.substring(0,2));
writeDocument(sentence.indexOf("class")); //检索字符串的位置
writeDocument(sentence.lastIndexOf("class"));
writeDocument(sentence.match("class"));
writeDocument(sentence.match(/class/g));
writeDocument(sentence.search(/class/g));
function countChar(stringToCount,char){
var charCount=0;
for(var i=0;i<stringToCount.length;i++){
var char=stringToCount.charAt(i);
if (char.toLowerCase() == "c") {
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);
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("1fdf4555"));
console