function writeDocument(variable) {
document.write(variable + "<br/>");
}
var welcomeString= "welcome";
writeDocument(welcomeString);
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);
writeDocument(sentence.replace("class", "programming class"));
writeDocument(sentence.replace(/class/g, "programming class"));
writeDocument("//split-----------------------------------------------");
var splitSentence = "We have fun in our class";
writeDocument(splitSentence);
writeDocument(splitSentence.split(" "));
writeDocument(welcomeString);
writeDocument(welcomeString.split(""));
writeDocument(sentence.split(""));
writeDocument("//IndexOf-----------------------------------------------");
writeDocument(sentence.indexOf("class"));
writeDocument(sentence.lastIndexOf("class"));
writeDocument(sentence.match("class"));
writeDocument(sentence.match(/class/g));
writeDocument(sentence.search(/class/g));
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.");
function countNumber(stringtoCount) {
var numCount = 0;
for (var i = 0; i < stringtoCount.length; ++i) {
var char = stringtoCount.charAt(i);
//isNaN()对空格字符会转化为0,需要加个判断charAt(i)不能为空格
if (char != " " && !isNaN(char)) {
numCount++;
}
}
return numCount;
}
document.write(countNumber("1234bhijv57689"));
console