JS字符串(学习笔记)

2018-05-01  本文已影响0人  Ashley2333
var text = 'hello ';
text = text + 'world'; // 也可以用+=表示:text+='world';
console.log(text); // hello world
var text = 'Hello \n World';  // 加入了一个换行符(\n)

效果:

"Hello 
 World"
var text = 'Hello ';
console.log(text.length); // 6 (注意Hello后面还有一个空格)
var text = 'tws ';
text = text + 'academy';

在这个过程中,首先会创建一个能容纳11个字符的新字符串,然后在这个字符串中填充'tws'和'academy',最后一步是销毁原来的字符串'tws'和字符串'academy',因为这两个字符串已经没用了。

字符串常用方法

var hello = "Hello, ";
console.log(hello.concat("tws", " have a nice day.")); // Hello, tws have a nice day.
console.log(hello); // Hello, 
'Blue Whale'.includes('blue'); // false (大小写不同)
'Blue Whale'.includes('Blue'); // true
var str = 'abcdefghij';
str.substring(0,3); // 'abc'
str.substring(3,3); // ''  (因为从3到3,中间没有字符)
str.substring(3); // 'defghij'
str.substring(2,3); // 'c'

练习

完成下面程序,将name变量中的字母全部转为为大写,输出:'HELLO'。

var name = 'hello';
// write your code here...

解:

var name = 'hello';
name=name.toUpperCase()
alert(name);
上一篇 下一篇

猜你喜欢

热点阅读