function writeDocument(variable){
document.write(variable+"<br/>");
}
var welcomeString="welcome";
writeDocument(welcomeString);
writeDocument(welcomeString.length);
//case
writeDocument(welcomeString.toLowerCase());
writeDocument(welcomeString.toUpperCase());
//charAt
writeDocument(welcomeString.charAt(2));
writeDocument(welcomeString.charAt(6));
//substring
writeDocument(welcomeString.substring(3));
writeDocument(welcomeString.substring(0,2));
document.write('<br/>-------------------------------<br/>');
var sentence="We have fun in our class.Do you have fun in your class?"
writeDocument(sentence);
//替换字符串replace
writeDocument(sentence.replace("We","You"));
//正则表达式split
writeDocument(sentence.replace(/class/g,"programming class"));
document.write('<br/>-------------------------------<br/>');
var splitSentence="We have fun in our class";
writeDocument(splitSentence);
writeDocument(splitSentence.split(" "));
writeDocument(welcomeString);
writeDocument(welcomeString.split(""));
writeDocument(sentence);
writeDocument(sentence.split(" "));
//IndexOf某字符串首次出现,lastIndexOf字符串最后出现的下标位置
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/>');
//统计c字符的个数
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("统计的'c'的个数为:"+countChar(stringValue,"c"))
document.write('<br/>-------------------------------<br/>');
//统计数字个数
function countNumber(stringtoCount,char) {
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;
}
var stringtoCount="47asdcgij765hji5";
writeDocument("统计的数字个数为:"+countNumber(stringtoCount));
console