Featured image of post 【JavaScript 变数】变数类型介绍:String 字串

【JavaScript 变数】变数类型介绍:String 字串

【JavaScript 变数】变数类型介绍:String 字串

Photo by Mohammad Rahmani on Unsplash

变数加入字串中

在设定字串时使用键盘左上角 重音符 将字串括起来,然后中间要插入的变数使用 ${变数名称} 即可将变数资料插入到字串中间

const my_name = 'KJ';
// 将其他变数加入字串中
const message = `Hi, my name is ${my_name}`;
console.log(message);

字串函式

变数 说明
String.slice() 分割字串
String.substring() 分割字串
String.replace() 取代字串
String.charAt() 字串第几个字
String.charCodeAt() 字串第几个字的 UTF-16 code
String.fromCharCode() 从 UTF-16 code 转换成字串
String.codePointAt() 字串第几个字的 Unicode code
String.indexOf() 字串在第几个索引位置,从 0 开始数,若找不到回传 -1
String.search() 搜寻符合正规表示式文字位置
String.match() 符合正规表示式的文字,有找到回传阵列,没找到回传 null
String.matchAll() 符合正规表示式的文字,有找到回传阵列,没找到回传 null,有更详细的比对资讯
String.split() 切割字串
String.padEnd() 填入字串到字尾
String.padStart() 填入字串到字首
String.concat() 连接字串
String.startsWith() 搜寻起始字串是否为指定字串
String.endsWith() 搜寻结为字串是否为指定字串
String.trim() 去除前后空白
String.trimEnd() 去除后方空白
String.trimStart() 去除前方空白
String.repeat() 重複字串
String.toLowerCase() 转换成小写
String.toUpperCase() 转换成大写
String.toString() 转换成字串类型变数
String.includes() 判断字串是否存在(大小写不同)
String.matchAll() 取得符合正规表示式的字串阵列

String.slice() 分割字串

const str = 'JavaScript';

console.log(str.slice(1, 3));
// expected output: "av"

console.log(str.slice(2));
// expected output: "vaScript"

String.substring() 分割字串

const str = 'JavaScript';

console.log(str.substring(1, 3));
// expected output: "av"

console.log(str.substring(2));
// expected output: "vaScript"

String.chatAt() 字串第几个字

let my_name = "Hi, My name is KJ";
// M
console.log(my_name.charAt(4));

charCodeAt 字串第几个字的 UTF-16 code

let my_name = "Hi, My name is KJ";
// 77
console.log(my_name.charCodeAt(4));

取得指定字串的 Ascii

// 56
console.log('A'.charCodeAt(0));
// 97
console.log('a'.charCodeAt(0));
// 98
console.log('abc'.charCodeAt(1));

String.fromCharCode() 从 UTF-16 code 转换成字串

// a
String.fromCharCode(97);
// b
String.fromCharCode(98);
// c
String.fromCharCode(99);

String.indexOf() 字串在第几个索引位置

从 0 开始数,若找不到回传 -1

let my_name = "Hi, My name is KJ";
// 15
console.log(my_name.indexOf('KJ'));
let my_name = "Hi, My name is KJ";
// -1
console.log(my_name.indexOf('Unknown'));

String.match() 符合正规表示式的文字

有找到回传阵列,没找到回传 null

const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
const regex = /[A-Z]/g;
const found = paragraph.match(regex);
// [ 'T', 'I' ]
console.log(found);

const paragraph = 'the quick brown fox jumps over the lazy dog. it barked.';
const regex = /[A-Z]/g;
const found = paragraph.match(regex);
// null
console.log(found);

String.split() 切割字串

const message = 'Hi, my name is Kay Jay';

// 从空白切割
const words = message.split(' ');
// [ 'Hi,', 'my', 'name', 'is', 'Kay', 'Jay' ]
console.log(words);

// 切割每个字元
const chars = message.split('');
// [
//     'H', 'i', ',', ' ', 'm',
//     'y', ' ', 'n', 'a', 'm',
//     'e', ' ', 'i', 's', ' ',
//     'K', 'a', 'y', ' ', 'J',
//     'a', 'y'
// ]
console.log(chars);

// 複製字串成一个阵列
const messageCopyArray = message.split();
// [ 'Hi, my name is Kay Jay' ]
console.log(messageCopyArray);

String.padEnd() 填入字串到字尾

const my_name = 'KJ';

// 填入指定字串到字尾
// "KJ....."
console.log(my_name.padEnd(7, '.'));

// 填入空白到字尾
// "KJ     "
console.log(my_name.padEnd(7));

String.padStart() 填入字串到字首

const my_name = 'KJ';

// 填入指定字串到字首
// ".....KJ"
console.log(my_name.padStart(7, '.'));

// 填入空白到字首
// "     KJ"
console.log(my_name.padStart(7));

String.trim() 去除前后空白

const my_name = '   KJ   ';

// "   KJ   "
console.log(my_name);

// "KJ"
console.log(my_name.trim());

String.repeat() 重複字串

let my_name = "KJ";
// KJKJKJ
console.log(my_name.repeat(3));

Reference

Donate KJ 贊助作者喝咖啡

如果這篇文章對你有幫助的話,可以透過下面支付方式贊助作者喝咖啡,如果有什麼建議或想說的話可以贊助並留言給我
If this article has been helpful to you, you can support the author by treating them to a coffee through the payment options below. If you have any suggestions or comments, feel free to sponsor and leave a message for me!
方式 Method 贊助 Donate
PayPal https://paypal.me/kejyun
綠界 ECPay https://p.ecpay.com.tw/AC218F1
歐付寶 OPay https://payment.opay.tw/Broadcaster/Donate/BD2BD896029F2155041C8C8FAED3A6F8
All rights reserved,未經允許不得隨意轉載
Built with Hugo
主题 StackJimmy 设计