Voca:终极 JavaScript 字符串操作库


Voca 是一个用于操作字符串的 JavaScript 库。在本教程中,我们将通过多个示例来展示如何使用 Voca 中提供的不同函数。

Voca 的特性

在查看所有示例之前,让我们先重点介绍 Voca 提供的一些特性:

  • 它提供了大量可用于操作、查询、转义和格式化字符串的函数。

  • 它还提供详细且可搜索的文档。

  • 它支持各种环境,如 Node.js、Safari 7+、Chrome、Firefox 等。

  • 它不需要任何依赖项。

如何安装 Voca?

现在我们已经了解了 Voca.js 的特性,让我们看看如何在本地机器上安装它。要安装 Voca,请在终端中运行以下命令:

npm install voca

在终端中运行上述命令后,将创建一个 "package.json" 文件以及一个 "package-lock.json" 文件和一个 "node_modules" 文件夹。现在,我们就可以在代码中使用 Voca 函数了。

由于我们将讨论 Voca 的许多函数,因此最好将它们分成不同的常用类别。

Learn JavaScript in-depth with real-world projects through our JavaScript certification course. Enroll and become a certified expert to boost your career.

更改字符串大小写

我们将探索的第一类示例是**大小写**,其中我们将更改特定文本的大小写。

camelCase() 函数

当我们希望将文本转换为其驼峰式表示形式时,可以使用**camelCase()** 函数。请考虑以下代码。

const v = require('voca');
let companyName = 'tutorials point';
console.log("Input Text -", companyName);
console.log('Camel Case -', v.camelCase(companyName));

要运行上述代码,首先将其保存为名为 "index.js" 的文件,然后运行以下命令。

node index.js

它将产生以下**输出**

Input Text - tutorials point
Camel Case - tutorialsPoint

capitalize() 函数

当我们希望将文本转换为其首字母大写表示形式时,可以使用**capitalize()** 函数。请考虑以下代码。

const v = require('voca');
let companyName = 'tutorials point';
console.log('Input Text –', companyName);
console.log('Capitalize –', v.capitalize(companyName));

它将产生以下**输出**

Input Text – tutorials point
Capitalize – Tutorials point

decapitalize() 函数

当我们希望将文本转换为其首字母小写表示形式时,可以使用**decapitalize()** 函数。请考虑以下代码。

const v = require('voca');
let companyName = 'Tutorials point';
console.log('Input - ', companyName);
console.log('Decapitalize -', v.decapitalize(companyName));

它将产生以下**输出**

Input - Tutorials point
Decapitalize - tutorials point

kebabCase() 函数

当我们希望将文本转换为其 kebabCase 表示形式时,可以使用**kebabCase()** 函数。请考虑以下代码。

const v = require('voca');
let companyName = 'tutorials point';
console.log('Input -', companyName);
console.log('KebabCase -', v.kebabCase(companyName));

它将产生以下**输出**

Input - tutorials point
KebabCase - tutorials-point

snakeCase() 函数

当我们希望将文本转换为其 snakeCase 表示形式时,可以使用**snakeCase()** 函数。请考虑以下代码。

const v = require('voca');
let companyName = 'tutorials point';
console.log('Input -', companyName);
console.log('snakeCase -', v.snakeCase(companyName));

它将产生以下**输出**

Input - tutorials point
snakeCase - tutorials_point

lowerCase() 函数

当我们希望将文本转换为其小写表示形式时,可以使用**lowerCase()** 函数。请考虑以下代码。

const v = require('voca');
let companyName = 'TUTORIALS point';
console.log('Input -', companyName);
console.log('LowerCase -', v.lowerCase(companyName));

它将产生以下**输出**

Input - TUTORIALS point
LowerCase - tutorials point

swapCase() 函数

当我们希望将文本转换为其大小写互换表示形式时,可以使用**swapCase()** 函数。请考虑以下代码。

const v = require('voca');
let companyName = 'tutorials point';
console.log('Input -', companyName);
console.log('SwapCase -', v.swapCase(companyName));

它将产生以下**输出**

Input - tutorials point
SwapCase - TUTORIALS POINT

titleCase() 函数

当我们希望将文本转换为其标题大小写表示形式时,可以使用**titleCase()** 函数。请考虑以下代码。

const v = require('voca');
let companyName = 'tutorials point';
console.log('Input -', companyName);
console.log('TitleCase -', v.titleCase(companyName));

它将产生以下**输出**

Input - tutorials point
TitleCase - Tutorials Point

upperCase() 函数

upperCase() 函数用于将文本转换为其大写表示形式。请考虑以下代码。

const v = require('voca');
let companyName = 'tutorials point';
console.log('Input -', companyName);
console.log('UpperCase -', v.upperCase(companyName));

它将产生以下**输出**

Input - tutorials point
UpperCase - TUTORIALS POINT

使用 Voca 进行链式调用

链式调用是指我们可以将多个函数一个接一个地进行链式调用。请考虑以下代码。

const v = require('voca');
let str = 'Tutorials Point is Awesome!';
console.log('Creating a chain object -', v(str).lowerCase().words());
console.log('Chaining and Wrapping -', v.chain(str).lowerCase().words().value());

它将产生以下**输出**

Creating a chain object - [ 'tutorials', 'point', 'is', 'awesome' ]
Chaining and Wrapping - [ 'tutorials', 'point', 'is', 'awesome' ]

使用 Voca 进行截取

截取包括**charAt()、first()、last()** 等字符串操作函数。

charAt() 函数

当我们希望获取特定索引处的字符时,可以使用**charAt()** 函数。请考虑以下代码。

const v = require('voca');
let thingsILove = 'Formula1-Football-Leetcode-Sleeping';
console.log('Input String -', thingsILove);
console.log('charAt 10th index -', v.charAt(thingsILove, 10));
console.log('charAt 7th index -', v.charAt(thingsILove, 7));

它将产生以下**输出**

Input String - Formula1-Football-Leetcode-Sleeping
charAt 10th index - o
charAt 7th index - 1

first() 函数

当我们希望从文本中提取第一个字符时,可以使用**first()** 函数。请考虑以下代码。

const v = require('voca');
let thingsILove = 'Formula1-Football-Leetcode-Sleeping';
console.log('Input -', thingsILove);
console.log('first -', v.first(thingsILove));
console.log('first -', v.first(thingsILove, 8));

它将产生以下**输出**

Input - Formula1-Football-Leetcode-Sleeping
first - F
first - Formula1

last() 函数

当我们希望从文本中提取最后一个字符时,可以使用**last()** 函数。请考虑以下代码。

const v = require('voca');
let thingsILove = 'Formula1-Football-Leetcode-Sleeping';
console.log('Input -', thingsILove);
console.log('last -', v.last(thingsILove));
console.log('last -', v.last(thingsILove, 8));

它将产生以下**输出**

Input - Formula1-Football-Leetcode-Sleeping
last - g
last - Sleeping

使用 Voca 进行切片

当我们希望从文本中提取切片时,可以使用**slice()** 函数。请考虑以下代码。

const v = require('voca');
console.log(v.slice('Delhi', 1));
console.log(v.slice('India', -4));

它将产生以下**输出**

elhi
ndia

使用 Voca 提取子字符串

当我们希望从文本中提取子字符串时,可以使用**substring()** 函数。最后一个元素也将被包含在内。请考虑以下代码。

const v = require('voca');
console.log(v.substring('Delhi', 3));
console.log(v.substring('India', 2, 4));

它将产生以下**输出**

hi
di

Voca 中的 Count 函数

当我们希望计算文本中存在的单词数量时,可以使用**count()** 函数。请考虑以下代码。

const v = require('voca');
console.log(v.count('Delhi'));

它将产生以下**输出**

5

计算子字符串的数量

当我们希望计算文本中存在的子字符串数量时,可以使用**countSubstrings()** 函数。请考虑以下代码。

const v = require('voca');
console.log(v.countSubstrings('India is beautiful. India is huge!', 'India'));

它将产生以下**输出**

2

Voca 中的 Index 函数

在与索引相关的函数中,我们将使用**indexOf()** 函数,该函数主要用于当我们希望查找特定字符串在文本中存在的特定索引时。请考虑以下示例。

console.log(v.indexOf('India', 'n'));
console.log(v.indexOf('India', 'p'));
console.log(v.indexOf('Leetcode', 'e'));

它将产生以下**输出**

1
-1
1

请注意,在第二种情况下,输入字符串中不存在字母“p”,因此它返回“-1”作为输出。

Voca 中的 Insert 函数

当我们希望在文本之间插入特定文本时,可以使用**insert()** 函数。请考虑以下示例。

const v = require('voca');
console.log(v.insert('cde','o',1));

它将产生以下**输出**

code

它在给定字符串的“1”位置插入了字母“o”。

Vocac 中的 Repeat 函数

当我们希望重复特定文本多次时,可以使用**repeat()** 函数。请考虑以下示例。

const v = require('voca');
console.log(v.repeat('a', 3));

它将产生以下**输出**

aaa

使用 Voca 反转字符串

当我们希望反转特定文本时,可以使用**reverse()** 函数。请考虑以下示例。

const v = require('voca');
console.log(v.reverse('apple'));

它将产生以下**输出**

elppa

使用 Voca 修剪字符串

当我们希望从文本的左侧和右侧修剪特定文本时,可以使用**trim()** 函数。请考虑以下示例。

const v = require('voca');
console.log(v.trim(' an apple falling too down under '));

在上面的示例中,我们可以看到文本的两侧有一些额外的空格(空白),我们可以使用 Voca 包中提供的 trim() 函数将其删除。

它将产生以下**输出**

an apple falling too down under

检查字符串是否为空

当我们希望检查特定文本是否为空时,可以使用**isEmpty()** 函数。请考虑以下示例。

const v = require('voca');
console.log(v.isEmpty(''));

它将产生以下**输出**

true

它返回“true”,因为输入字符串为空。

检查字符串是否为数字类型

当我们希望检查特定文本是否为数字类型时,可以使用**isNumeric()** 函数。请考虑以下示例。

const v = require('voca');
console.log(v.isNumeric('Hey there'));
console.log(v.isNumeric(3));

它将产生以下**输出**

false
true

检查文本是否为字符串类型

当我们希望检查特定文本是否为字符串类型时,可以使用**isString()** 函数。请考虑以下示例。

const v = require('voca');

console.log(v.isString('Hey there'));
console.log(v.isString(12345));

它将产生以下**输出**

true
false

在第一种情况下,它返回“true”,因为输入文本为字符串类型。在第二种情况下,输入文本为整数类型,因此它返回“false”。

Voca 中的 startsWith 函数

当我们希望检查特定文本是否以特定文本开头时,可以使用**startsWith()** 函数。请考虑以下示例。

const v = require('voca');
console.log(v.startsWith('Hey there, join us?', 'Hey'));

它将产生以下**输出**

true

输入字符串以子字符串“Hey”开头,因此它返回“true”。

Voca 中的 endsWith 函数

当我们希望检查特定文本是否以特定文本结尾时,可以使用**endsWith()** 函数。请考虑以下示例。

const v = require('voca');
console.log(v.endsWith('Hey there, join us?', 'us?'));

它将产生以下**输出**

true

在这里,我们正在检查输入字符串是否以子字符串“us?”结尾。它返回“true”,因为输入字符串确实以给定的子字符串结尾。

Voca 中的 includes() 函数

**includes()**

函数用于当我们希望检查特定文本是否包含指定文本时。请考虑以下示例。

const v = require('voca');
console.log(v.includes('Hey there, join us?', 'oin'));

它将产生以下**输出**

true

在这里,输入字符串包含给定的子字符串“oin”,因此它返回“true”。

结论

在本教程中,我们使用了一些示例来演示如何利用 Voca 中的一些流行的字符串操作函数。

更新于: 2022年10月11日

395 次查看

开启你的职业生涯

通过完成课程获得认证

立即开始
广告