码迷,mamicode.com
首页 > 其他好文 > 详细

字符串

时间:2018-11-24 21:04:11      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:test   变量   ase   结束   asd   pre   es6   ring   wiki   

用 ‘‘ "" 表示

用\"或\‘表示为字符,转义字符\标记

\x## 表示十六进制

如 \x41 = A

\u####表示Unicode字符;

多行字符串: ··表示(ES6新标准)

部分字符串操作:

"use strict";
var x =1, y = 2;

var name = "小明";
var age = 18;
var message = "hello ,"+name +","+age +"你好";
var test = `2333

3333`;
console.log(message);
console.log(test);
var string = "asdxuhaoran ";
console.log("长度:"+test.length);
console.log(string.toUpperCase());
console.log(string[2]);
// hello ,小明,18你好
// 2333
//
// 3333
// 长度:10
// ASDXUHAORAN
// d

 

JavaScript为字符串提供了一些常用方法,注意,调用这些方法本身不会改变原有字符串的内容,而是返回一个新字符串:

toUpperCase

 

toUpperCase()把一个字符串全部变为大写:

var s = ‘Hello‘;
s.toUpperCase(); // 返回‘HELLO‘

 

toLowerCase

toLowerCase()把一个字符串全部变为小写:

var s = ‘Hello‘;
var lower = s.toLowerCase(); // 返回‘hello‘并赋值给变量lower
lower; // ‘hello‘

 

indexOf

indexOf()会搜索指定字符串出现的位置:

var s = ‘hello, world‘;
s.indexOf(‘world‘); // 返回7
s.indexOf(‘World‘); // 没有找到指定的子串,返回-1

 

substring

substring()返回指定索引区间的子串:

var s = ‘hello, world‘
s.substring(0, 5); // 从索引0开始到5(不包括5),返回‘hello‘
s.substring(7); // 从索引7开始到结束,返回‘world‘

参考:https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/001434499203693072018f8878842a9b0011e3ff4e38b6b000

 

字符串

标签:test   变量   ase   结束   asd   pre   es6   ring   wiki   

原文地址:https://www.cnblogs.com/donke/p/10013203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!