/**********************************************************************
** author : Bugliu
** date : 2018-12-04
** description : 字符串
** 学习网站 : www.liaoxuefeng.com
**********************************************************************/
/* 字符串 */
// 字符串用''或""括起来表示,如果'本身是字符,可以用""括起来。
console.log("I'm ok"); // I'm ok
// 如果' 与"都是字符, 可使用转义字符\
console.log("I'm \"ok\"!"); // I'm "ok"
// ASCII字符可以以\x##形式的十六进制表示
console.log('\x41'); // 等同于'A'
// Unicode字符表示
console.log('\u4e2d\u6587'); // 等同于'中文'
// 多行字符串 用反引号`...`
console.log(`这是一个
多行字符串`);
// 字符串连接
var name = 'bob';
var age = 20;
console.log(name+' is '+age); // bob is 20
console.log(`${name} is ${age}`); // bob is 20
// 字符串操作
var str = "hello, world";
// 取长度
console.log(str.length); // 12
console.log(str[0]); // h
console.log(str[12]); // 索引越界不报错,但返回undefined
// 字符串是不可变的,如果对字符串的某个索引赋值,不会有任何错误,
// 但是,也没有任何效果
str[0] = 'H';
console.log(str[0]); // h
// 常用方法
console.log(str.toUpperCase()); //HELLO, WORLD
console.log(str.toLowerCase()); //hello, world
console.log(str.indexOf('world')); //7
console.log(str.substring(0, 5)); //hello
console.log(str.substring(7)); //world
JavaScript 学习笔记2_字符串_20181204
最新推荐文章于 2025-05-05 10:50:39 发布
本文深入讲解了JavaScript中字符串的各种操作方法,包括基本的字符串表示、转义字符的使用、多行字符串、字符串连接、字符串长度获取、索引访问、字符串不可变性特性,以及常用的字符串方法如toUpperCase、toLowerCase、indexOf和substring等。
1592

被折叠的 条评论
为什么被折叠?



