function writeDocument(variable)
{
document.write(variable + "<br/>");
}
var welcomeString = "welcome";
writeDocument(welcomeString);
writeDocument(welcomeString.length);
writeDocument(welcomeString.toLowerCase());
writeDocument(welcomeString.toUpperCase());
writeDocument(welcomeString.charAt(2));
writeDocument(welcomeString.charAt(6));
writeDocument(welcomeString.substring(3));
writeDocument(welcomeString.substring(0,2));
//替换字符串
var sentence = "We have fun in our class. Do you have fun in your class?"
writeDocument(sentence);
writeDocument(sentence.replace("We","You"));
writeDocument(sentence.replace(/class/g,"programming class"));
//分割字符串
var splitSentence = "We have fun in our class";
writeDocument(splitSentence);
writeDocument(splitSentence.split(" "));
writeDocument(welcomeString);
writeDocument(welcomeString.split(""));
writeDocument(sentence.split("."));
//检索字符串的位置
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") + " in sentence.");
//统计字符串的数字的个数
function getNum(str) {
var num = 0;
for (var i = 0; i < str.length; i++)
{
var char = str.charAt(i);
//isNaN()对空格字符会转化为0,需要加个判断charAt(i)不能为空格
//不能为空格字符,isNaN()对数字会返回false
if (char != " " && !isNaN(char))
{
num++;
}
}
return num;
}
document.write(getNum("yvbn3fr767fyt0xffb"));
console