Table of Contents
Voca’s functions
How to install Voca?
Change string case
camelCase() function
capitalize() function
decapitalize() function
kebabCase() function
snakeCase() function
lowerCase() function
swapCase() function
titleCase() function
upperCase() function
Use Voca for linking
Chopping with Voca
charAt() function
first() function
last() function
Using Voca for slicing
Use Voca to extract substrings
Voca 中的计数函数
计算子字符串的数量
Voca 中的索引函数
在 Voca 中插入函数
Vocac 中的重复函数
使用 Voca 反转字符串
使用 Voca 修剪字符串
检查字符串是否为空
检查字符串是否为数字类型
检查文本是否为字符串类型
Voca 中的startsWith 函数
Voca中的endsWith函数
Voca 中的 include() 函数
结论
Home Web Front-end JS Tutorial Voca: The ultimate Javascript library for string manipulation

Voca: The ultimate Javascript library for string manipulation

Aug 30, 2023 pm 05:45 PM

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 -

1

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.

1

2

3

4

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.

1

node index.js

Copy after login

It will produce the following output

1

2

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.

1

2

3

4

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

1

2

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.

1

2

3

4

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

1

2

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.

1

2

3

4

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

1

2

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.

1

2

3

4

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

1

2

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.

1

2

3

4

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

1

2

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.

1

2

3

4

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

1

2

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.

1

2

3

4

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

1

2

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.

1

2

3

4

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

1

2

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.

1

2

3

4

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

1

2

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.

1

2

3

4

5

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

1

2

3

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.

1

2

3

4

5

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

1

2

3

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.

1

2

3

4

5

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

1

2

3

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.

1

2

3

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

1

2

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.

1

2

3

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

1

2

hi

di

Copy after login

Voca 中的计数函数

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

1

2

const v = require('voca');

console.log(v.count('Delhi'));

Copy after login

它将产生以下输出

1

5

Copy after login

计算子字符串的数量

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

1

2

const v = require('voca');

console.log(v.countSubstrings('India is beautiful. India is huge!', 'India'));

Copy after login

它将产生以下输出

1

2

Copy after login

Voca 中的索引函数

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

1

2

3

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

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

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

Copy after login

它将产生以下输出

1

2

3

1

-1

1

Copy after login

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

在 Voca 中插入函数

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

1

2

const v = require('voca');

console.log(v.insert('cde','o',1));

Copy after login

它将产生以下输出

1

code

Copy after login

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

Vocac 中的重复函数

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

1

2

const v = require('voca');

console.log(v.repeat('a', 3));

Copy after login

它将产生以下输出

1

aaa

Copy after login

使用 Voca 反转字符串

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

1

2

const v = require('voca');

console.log(v.reverse('apple'));

Copy after login

它将产生以下输出

1

elppa

Copy after login

使用 Voca 修剪字符串

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

1

2

const v = require('voca');

console.log(v.trim(' an apple falling too down under '));

Copy after login

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

它将产生以下输出

1

an apple falling too down under

Copy after login

检查字符串是否为空

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

1

2

const v = require('voca');

console.log(v.isEmpty(''));

Copy after login

它将产生以下输出

1

true

Copy after login
Copy after login
Copy after login
Copy after login

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

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

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

1

2

3

const v = require('voca');

console.log(v.isNumeric('Hey there'));

console.log(v.isNumeric(3));

Copy after login

它将产生以下输出

1

2

false

true

Copy after login

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

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

1

2

3

4

const v = require('voca');

 

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

console.log(v.isString(12345));

Copy after login

它将产生以下输出

1

2

true

false

Copy after login

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

Voca 中的startsWith 函数

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

1

2

const v = require('voca');

console.log(v.startsWith('Hey there, join us?', 'Hey'));

Copy after login

它将产生以下输出

1

true

Copy after login
Copy after login
Copy after login
Copy after login

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

Voca中的endsWith函数

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

1

2

const v = require('voca');

console.log(v.endsWith('Hey there, join us?', 'us?'));

Copy after login

它将产生以下输出

1

true

Copy after login
Copy after login
Copy after login
Copy after login

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

Voca 中的 include() 函数

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

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

1

2

const v = require('voca');

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

Copy after login

它将产生以下输出

1

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig

See all articles