Home > Web Front-end > JS Tutorial > Voca: The ultimate Javascript library for string manipulation

Voca: The ultimate Javascript library for string manipulation

WBOY
Release: 2023-08-30 17:45:12
forward
1674 people have browsed it

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

Voca is a JavaScript library for manipulating strings. In this tutorial, we will use several examples to show how to use the different features available in Voca.

Voca’s functions

Before looking at all the examples, let’s highlight some of the features Voca brings -

  • It provides a large number of functions that can be used to operate, query, escape, and format strings.

  • It also provides detailed and searchable documentation.

  • It supports a wide range of environments, such as Node, js, Safari 7, Chrome, Firefox, etc.

  • It does not require any dependencies

How to install Voca?

Now that we understand what Voca.js does, let’s see how to install it on our local computer. To install Voca, run the following command in the terminal -

npm install voca
Copy after login

After running the above command in the terminal, a "package.json" file will be created along with "package-lock.json" and a "node_modules" folder. Now, we're ready to use the Voca function in our code.

Since we will be discussing many of Voca’s features, it’s a good idea to break them down into different common categories.

Change string case

The first type of example we will explore is the case where we change the case of specific text.

camelCase() function

The camelCase() function is used when we want to convert text to its camelCase representation. Consider the code shown below.

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

To run the above code, first save it with the name "index.js" and then run the following command.

node index.js
Copy after login

It will produce the following output

Input Text - tutorials point
Camel Case - tutorialsPoint
Copy after login

capitalize() function

The capitalize() function is used when we want to convert text to its uppercase representation. Consider the code shown below.

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

It will produce the following output

Input Text – tutorials point
Capitalize – Tutorials point
Copy after login

decapitalize() function

The decapitalize() function is used when we want to convert text to its non-capitalized representation. Consider the code shown below.

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

It will produce the following output

Input - Tutorials point
Decapitalize - tutorials point
Copy after login

kebabCase() function

The kebabCase() function is used when we want to convert text to its kebabCase representation. Consider the code shown below.

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

It will produce the following output

Input - tutorials point
KebabCase - tutorials-point
Copy after login

snakeCase() function

The snakeCase() function is used when we want to convert text into its snakeCake representation. Consider the code shown below.

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

It will produce the following output

Input - tutorials point
snakeCase - tutorials_point
Copy after login

lowerCase() function

The lowerCase() function is used when we want to convert text to its lowercase representation. Consider the code shown below.

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

It will produce the following output

Input - TUTORIALS point
LowerCase - tutorials point
Copy after login

swapCase() function

The swapCase() function is used when we want to convert text to its swapCase representation. Consider the code shown below.

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

It will produce the following output

Input - tutorials point
SwapCase - TUTORIALS POINT
Copy after login

titleCase() function

The titleCase() function is used when we want to convert text to its titleCase representation. Consider the code shown below.

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

It will produce the following output

Input - tutorials point
TitleCase - Tutorials Point
Copy after login

upperCase() function

The upperCase() function is used when we want to convert text to its uppercase representation. Consider the code shown below.

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

It will produce the following output

Input - tutorials point
UpperCase - TUTORIALS POINT
Copy after login

Use Voca for linking

Linking means that we can link multiple functions one after another. Consider the code shown below.

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());
Copy after login

It will produce the following output

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

Chopping with Voca

Chopping includes string manipulation functions, such as charAt(), first(), last(), etc.

charAt() function

When we want to get the character that appears at a specific index, use the charAt() function. Consider the code shown below.

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));
Copy after login

It will produce the following output

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

first() function

When we want to extract the first character from the text, use the first() function. Consider the code shown below.

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));
Copy after login

It will produce the following output

Input - Formula1-Football-Leetcode-Sleeping
first - F
first - Formula1
Copy after login

last() function

When we want to extract the last character from the text, use the last() function. Consider the code shown below.

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));
Copy after login

It will produce the following output

Input - Formula1-Football-Leetcode-Sleeping
last - g
last - Sleeping
Copy after login

Using Voca for slicing

When we want to extract a slice from text, use the slice() function. Consider the code shown below.

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

It will produce the following output

elhi
ndia
Copy after login

Use Voca to extract substrings

When we want to extract a substring from text, use the substring() function. The last element will also be included. Consider the code shown below.

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

It will produce the following output

hi
di
Copy after login

Voca 中的计数函数

当我们想要计算文本中出现的单词数时,使用count()函数。考虑下面所示的代码。

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

它将产生以下输出

5
Copy after login

计算子字符串的数量

当我们想要计算文本中存在的子字符串数量时,将使用 countSubstrings() 函数。考虑下面所示的代码。

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

它将产生以下输出

2
Copy after login

Voca 中的索引函数

在与索引相关的方法中,我们将使用 indexOf() 函数,该函数主要在我们想要查找特定字符串出现在文本中的特定索引时使用。考虑下面所示的示例。

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

它将产生以下输出

1
-1
1
Copy after login

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

在 Voca 中插入函数

当我们想要在文本之间插入特定文本时,使用insert()函数。考虑下面所示的示例。

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

它将产生以下输出

code
Copy after login

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

Vocac 中的重复函数

当我们想要多次重复特定文本时,可以使用repeat()函数。考虑下面所示的示例。

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

它将产生以下输出

aaa
Copy after login

使用 Voca 反转字符串

当我们想要反转特定文本时,使用reverse()函数。考虑下面所示的示例。

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

它将产生以下输出

elppa
Copy after login

使用 Voca 修剪字符串

当我们想要从文本的左侧和右侧修剪特定文本时,使用trim()函数。考虑下面所示的示例。

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

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

它将产生以下输出

an apple falling too down under
Copy after login

检查字符串是否为空

当我们想要检查特定文本是否为空时,使用isEmpty()函数。考虑下面所示的示例。

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

它将产生以下输出

true
Copy after login
Copy after login
Copy after login
Copy after login

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

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

当我们想要检查特定文本是否为数字类型时,使用isNumeric()函数。考虑下面所示的示例。

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

它将产生以下输出

false
true
Copy after login

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

当我们想要检查特定文本是否是字符串类型时,使用isString()函数。考虑下面所示的示例。

const v = require('voca');

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

它将产生以下输出

true
false
Copy after login

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

Voca 中的startsWith 函数

当我们想要检查特定文本是否以文本开头时,使用 startsWith() 函数。考虑下面所示的示例。

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

它将产生以下输出

true
Copy after login
Copy after login
Copy after login
Copy after login

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

Voca中的endsWith函数

当我们想要检查特定文本是否以文本结尾时,使用endsWith()函数。考虑下面所示的示例。

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

它将产生以下输出

true
Copy after login
Copy after login
Copy after login
Copy after login

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

Voca 中的 include() 函数

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

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

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

它将产生以下输出

true
Copy after login
Copy after login
Copy after login
Copy after login

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

结论

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

The above is the detailed content of Voca: The ultimate Javascript library for string manipulation. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template