如何声明变量以及如何使用子字符串

时间:2014-04-25 08:51:35

标签: javascript

var myCountry="india";

console.log( myCountry);

myCountry.substring(0,3)

console.log( myCountry);

这里我声明了一个变量myCountry.I已经将值作为india.Used console.log来打印变量。用过的console.log打印出myCountry的前三个字母。 编译器抛出的错误没有将myCountry的长度记录到控制台。

3 个答案:

答案 0 :(得分:2)

substring不会改变原始变量,它会返回结果,所以:

myCountry = myCountry.substring(0, 3);
console.log(myCountry);  // "ind"

答案 1 :(得分:1)

试试这个:

myCountry = myCountry.substring(0,3);

答案 2 :(得分:1)

注意:substring()方法不会更改原始字符串。

myCountry = myCountry.substring(0,3);

http://www.w3schools.com/jsref/jsref_substring.asp