首页  >  文章  >  web前端  >  Voca:用于字符串操作的终极 Javascript 库

Voca:用于字符串操作的终极 Javascript 库

WBOY
WBOY转载
2023-08-30 17:45:121614浏览

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 的很多功能,因此最好将它们分为不同的常见类别。

更改字符串大小写

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

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() 函数

当我们想要将文本转换为它的snakeCake 表示形式时,会使用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 表示形式时,将使用 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 表示形式时,将使用 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 进行斩波

Chopping 包括字符串操作函数,如 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()函数。考虑下面所示的代码。

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 中的索引函数

在与索引相关的方法中,我们将使用 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()函数。考虑下面所示的示例。

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

它将产生以下输出

code

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

Vocac 中的重复函数

当我们想要多次重复特定文本时,可以使用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() 函数将其删除。 p>

它将产生以下输出

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”,因为输入文本是字符串类型。在第二种情况下,输入文本是 Integer 类型,因此返回“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 中的 include() 函数

当我们想要检查特定文本中是否包含指定文本时,可以使用includes()

函数。考虑下面所示的示例。

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

它将产生以下输出

true

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

结论

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

以上是Voca:用于字符串操作的终极 Javascript 库的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除