JavaScript - 文本格式化

这篇博客探讨了JavaScript中的字符串操作,包括字符串字面量、Unicode转义序列和多行模板文字的使用。此外,还介绍了ECMAScript Internationalization API中的日期时间、数值格式化及校对功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字符串

字符串字面表达

'foo'
"bar"

Hexadecimal转义序列

'\xA9' // "©"

Unicode转义序列

'\u00A9' // "©"

Unicode code point escape

'\u{2F804}'

// the same with simple Unicode escapes
'\uD87E\uDC04'

字符串对象 

字符串对象是对原始字符串数据类型的封装

const foo = new String('foo'); // Creates a String object
console.log(foo); // Displays: [String: 'foo']
typeof foo; // Returns 'object'

const firstString = '2 + 2'; // Creates a string literal value
const secondString = new String('2 + 2'); // Creates a String object
eval(firstString); // Returns the number 4
eval(secondString); // Returns the string "2 + 2"

const hello = 'Hello, World!';
const helloLength = hello.length;
hello[0] = 'L'; // This has no effect, because strings are immutable
hello[0]; // This returns "H"

字符串对象的方法

charAt, charCodeAt, codePointAt返回索引指定位置的字符或者字符编码
indexOf, lastIndexOf返回指定子字符串初次出现的位置,或者最后一次出现的位置
startsWith, endsWith, includes判断是否以某指定文字起始、结尾、或者包含某指定文字
concat连接两个字符串,以新字符串返回
fromCharCode, fromCodePoint基于Unicode值创建字符串
split依据指定的分割符,将字符串分割写入数组
slice将字符串切割为片段,返回新字符串
subString, subStr依据指定索引返回子字符串
match, replace, search同正则表达式一起工作
toLowerCase, toUpperCase大小写转换
normalize返回字符串的Unicode Normalization Form
repeat取得该字符串指定重复次数的字符串
trim

除去字符串起始和结尾的空白

 

 

 

 

 

 

 

 

 

 

 

 

 

多行模板文字

模板文字允许表达式嵌套在其中,模板文字使用回剔符号包围(``)

多行

console.log('string text line 1\n\
string text line 2');
// "string text line 1
// string text line 2"

console.log(`string text line 1
string text line 2`);
// "string text line 1
// string text line 2"

嵌套表达式

const five = 5;
const ten = 10;
console.log('Fifteen is ' + (five + ten) + ' and not ' + (2 * five + ten) + '.');
// "Fifteen is 15 and not 20."

const five = 5;
const ten = 10;
console.log(`Fifteen is ${five + ten} and not ${2 * five + ten}.`);
// "Fifteen is 15 and not 20."

国际化 Internationalization

Intl对象是ECMAScript Internationalization API

日期和时间格式化

const msPerDay = 24 * 60 * 60 * 1000;
 
// July 17, 2014 00:00:00 UTC.
const july172014 = new Date(msPerDay * (44 * 365 + 11 + 197));

const options = { year: '2-digit', month: '2-digit', day: '2-digit',
                hour: '2-digit', minute: '2-digit', timeZoneName: 'short' };
const americanDateTime = new Intl.DateTimeFormat('en-US', options).format;
 
console.log(americanDateTime(july172014)); // 07/16/14, 5:00 PM PDT

数值格式化

const gasPrice = new Intl.NumberFormat('en-US',
                        { style: 'currency', currency: 'USD',
                          minimumFractionDigits: 3 });
 
console.log(gasPrice.format(5.259)); // $5.259

const hanDecimalRMBInChina = new Intl.NumberFormat('zh-CN-u-nu-hanidec',
                        { style: 'currency', currency: 'CNY' });
 
console.log(hanDecimalRMBInChina.format(1314.25)); // ¥ 一,三一四.二五

校对

Collator对象用来比较和排序字符串

const names = ['Hochberg', 'Hönigswald', 'Holzman'];
 
const germanPhonebook = new Intl.Collator('de-DE-u-co-phonebk');
 
// as if sorting ["Hochberg", "Hoenigswald", "Holzman"]:
console.log(names.sort(germanPhonebook.compare).join(', '));
// logs "Hochberg, Hönigswald, Holzman"

const germanDictionary = new Intl.Collator('de-DE-u-co-dict');
 
// as if sorting ["Hochberg", "Honigswald", "Holzman"]:
console.log(names.sort(germanDictionary.compare).join(', '));
// logs "Hochberg, Holzman, Hönigswald"

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值