Home > Article > Web Front-end > Voca: The ultimate Javascript library for string manipulation
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.
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
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
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.
The first type of example we will explore is the case where we change the case of specific text.
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));
To run the above code, first save it with the name "index.js" and then run the following command.
node index.js
It will produce the following output
Input Text - tutorials point Camel Case - tutorialsPoint
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));
It will produce the following output
Input Text – tutorials point Capitalize – Tutorials point
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));
It will produce the following output
Input - Tutorials point Decapitalize - tutorials point
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));
It will produce the following output
Input - tutorials point KebabCase - tutorials-point
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));
It will produce the following output
Input - tutorials point snakeCase - tutorials_point
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));
It will produce the following output
Input - TUTORIALS point LowerCase - tutorials point
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));
It will produce the following output
Input - tutorials point SwapCase - TUTORIALS POINT
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));
It will produce the following output
Input - tutorials point TitleCase - Tutorials Point
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));
It will produce the following output
Input - tutorials point UpperCase - TUTORIALS POINT
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());
It will produce the following output
Creating a chain object - [ 'tutorials', 'point', 'is', 'awesome' ] Chaining and Wrapping - [ 'tutorials', 'point', 'is', 'awesome' ]
Chopping includes string manipulation functions, such as charAt(), first(), last(), etc.
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));
It will produce the following output
Input String - Formula1-Football-Leetcode-Sleeping charAt 10th index - o charAt 7th index - 1
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));
It will produce the following output
Input - Formula1-Football-Leetcode-Sleeping first - F first - Formula1
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));
It will produce the following output
Input - Formula1-Football-Leetcode-Sleeping last - g last - Sleeping
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));
It will produce the following output
elhi ndia
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));
It will produce the following output
hi di
当我们想要计算文本中出现的单词数时,使用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
在与索引相关的方法中,我们将使用 indexOf() 函数,该函数主要在我们想要查找特定字符串出现在文本中的特定索引时使用。考虑下面所示的示例。
console.log(v.indexOf('India', 'n')); console.log(v.indexOf('India', 'p')); console.log(v.indexOf('Leetcode', 'e'));
它将产生以下输出
1 -1 1
请注意,在第二种情况下,搜索的输入字符串中不存在字母“p”,因此它返回“-1”作为输出。
当我们想要在文本之间插入特定文本时,使用insert()函数。考虑下面所示的示例。
const v = require('voca'); console.log(v.insert('cde','o',1));
它将产生以下输出
code
它在给定字符串的“1”位置插入了字母“o”。
当我们想要多次重复特定文本时,可以使用repeat()函数。考虑下面所示的示例。
const v = require('voca'); console.log(v.repeat('a', 3));
它将产生以下输出
aaa
当我们想要反转特定文本时,使用reverse()函数。考虑下面所示的示例。
const v = require('voca'); console.log(v.reverse('apple'));
它将产生以下输出
elppa
当我们想要从文本的左侧和右侧修剪特定文本时,使用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”。
当我们想要检查特定文本是否以文本开头时,使用 startsWith() 函数。考虑下面所示的示例。
const v = require('voca'); console.log(v.startsWith('Hey there, join us?', 'Hey'));
它将产生以下输出
true
输入字符串以子字符串“Hey”开头,因此返回“true”。
当我们想要检查特定文本是否以文本结尾时,使用endsWith()函数。考虑下面所示的示例。
const v = require('voca'); console.log(v.endsWith('Hey there, join us?', 'us?'));
它将产生以下输出
true
这里,我们检查输入字符串是否以子字符串“us?”结尾。它返回“true”,因为输入字符串确实以给定的子字符串结尾。
当我们想要检查特定文本中是否包含指定文本时,可以使用includes()
函数。考虑下面所示的示例。const v = require('voca'); console.log(v.includes('Hey there, join us?', 'oin'));
它将产生以下输出
true
这里,输入字符串包含给定的子字符串“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!