美文网首页
JavaScript-字符串

JavaScript-字符串

作者: Demetris | 来源:发表于2017-11-27 19:42 被阅读0次

    字符串

    1.表示

    。字符串用’’或””表示

    。当字符串本身带有’则可用””表示

    。当字符串既有’又有”,则使用转义字符\

    ‘I\’m  \“OK\”!’;

    2.多行字符串

    。字符串多行用\n表示—麻烦

    。ES6标准新增了反引号表示多行内容

    `这是一个

    多行

    字符串`;

    .输出结果为:

    这是一个

    多行

    字符串

    .反引号的表示位于ESC键下方,即数字键1左边

    [if !supportLists]3.[endif]模板字符串

    。多个字符串连接使用+号连接

    var name='Bob';

    var age=20;

    var info='His name is '+name+',and he is'+age+'years old';

    console.log(info);

    。ES6新增模板字符串

    var name=’Bob’;

    var age=20;

    var info=`His name is ${name},and he is ${age} years old`;——使用反引号

    console.log(info);

    [if !supportLists]4.[endif]字符串操作——var str=’hello world’;

    。长度——length

    str.length;  //13

    。获取字符串某个位置的字符——类似于Array操作

    str[0];  //h

    .字符串时不可变的,对字符串的某个人索引赋值不会有任何效果

    str[0]=’H’;

    alert(str);  //hello world

    。全部转换为大小写——toUpperCase()

    str.toUpperCase();  //HELLO WORLD

    。全部转换为小写——toLowerCase()

    str.toLowerCase();

    。搜索指定字符串出现的位置——indexOf()

    str.indexOf(‘world’);  //7

    str.indexOf(‘World’);   //-1

    。返回指定索引区间的字符串——subString()

    str.subString(0,5);   //hello

    str.ubString(7);   //world

    相关文章

      网友评论

          本文标题:JavaScript-字符串

          本文链接:https://www.haomeiwen.com/subject/cxzdbxtx.html