function writeDocument(variable)
{
document.write(variable + "<br/>");
}
var welcomeString="Welcome";
writeDocument(welcomeString);
writeDocument(welcomeString.length);
writeDocument("//---case----------------");
writeDocument(welcomeString.toLowerCase());
writeDocument(welcomeString.toUpperCase());
writeDocument("//---chatAt----------------");
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----------------");
writeDocument(sentence.split(" "));
writeDocument(welcomeString.split(""));
writeDocument("//---indexOf/lastIndexOf/match/search----------------");
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)
{
if(stringToCount.charAt(i.toLowerCase)==char)
{
++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 getNum(str)
{
var num = 0;
for (var i = 0; i < str.length; i++)
{
var char = str.charAt(i);
//isNaN()对空格字符会转化为0,需要加个判断charAt(i)不能为空格
if (char != " " && !isNaN(char))
{
num++;
}
}
return num;
}
document.write(getNum("1d3sdsg"));
console