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.
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 |
|
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 |
|
To run the above code, first save it with the name "index.js" and then run the following command.
1 |
|
It will produce the following output
1 2 |
|
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 |
|
It will produce the following output
1 2 |
|
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 |
|
It will produce the following output
1 2 |
|
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 |
|
It will produce the following output
1 2 |
|
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 |
|
It will produce the following output
1 2 |
|
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 |
|
It will produce the following output
1 2 |
|
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 |
|
It will produce the following output
1 2 |
|
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 |
|
It will produce the following output
1 2 |
|
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 |
|
It will produce the following output
1 2 |
|
Use Voca for linking
Linking means that we can link multiple functions one after another. Consider the code shown below.
1 2 3 4 |
|
It will produce the following output
1 2 |
|
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 |
|
It will produce the following output
1 2 3 |
|
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 |
|
It will produce the following output
1 2 3 |
|
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 |
|
It will produce the following output
1 2 3 |
|
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 |
|
It will produce the following output
1 2 |
|
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 |
|
It will produce the following output
1 2 |
|
Voca 中的计数函数
当我们想要计算文本中出现的单词数时,使用count()函数。考虑下面所示的代码。
1 2 |
|
它将产生以下输出
1 |
|
计算子字符串的数量
当我们想要计算文本中存在的子字符串数量时,将使用 countSubstrings() 函数。考虑下面所示的代码。
1 2 |
|
它将产生以下输出
1 |
|
Voca 中的索引函数
在与索引相关的方法中,我们将使用 indexOf() 函数,该函数主要在我们想要查找特定字符串出现在文本中的特定索引时使用。考虑下面所示的示例。
1 2 3 |
|
它将产生以下输出
1 2 3 |
|
请注意,在第二种情况下,搜索的输入字符串中不存在字母“p”,因此它返回“-1”作为输出。
在 Voca 中插入函数
当我们想要在文本之间插入特定文本时,使用insert()函数。考虑下面所示的示例。
1 2 |
|
它将产生以下输出
1 |
|
它在给定字符串的“1”位置插入了字母“o”。
Vocac 中的重复函数
当我们想要多次重复特定文本时,可以使用repeat()函数。考虑下面所示的示例。
1 2 |
|
它将产生以下输出
1 |
|
使用 Voca 反转字符串
当我们想要反转特定文本时,使用reverse()函数。考虑下面所示的示例。
1 2 |
|
它将产生以下输出
1 |
|
使用 Voca 修剪字符串
当我们想要从文本的左侧和右侧修剪特定文本时,使用trim()函数。考虑下面所示的示例。
1 2 |
|
在上面的示例中,我们可以看到文本两侧都存在一些额外的空格(空白),我们可以借助 Voca 包中提供的 trim() 函数将其删除。
它将产生以下输出
1 |
|
检查字符串是否为空
当我们想要检查特定文本是否为空时,使用isEmpty()函数。考虑下面所示的示例。
1 2 |
|
它将产生以下输出
1 |
|
当输入字符串为空时,它返回“true”。
检查字符串是否为数字类型
当我们想要检查特定文本是否为数字类型时,使用isNumeric()函数。考虑下面所示的示例。
1 2 3 |
|
它将产生以下输出
1 2 |
|
检查文本是否为字符串类型
当我们想要检查特定文本是否是字符串类型时,使用isString()函数。考虑下面所示的示例。
1 2 3 4 |
|
它将产生以下输出
1 2 |
|
在第一种情况下返回“true”,因为输入文本是字符串类型。在第二种情况下,输入文本是 Integer 类型,因此返回“false”。
Voca 中的startsWith 函数
当我们想要检查特定文本是否以文本开头时,使用 startsWith() 函数。考虑下面所示的示例。
1 2 |
|
它将产生以下输出
1 |
|
输入字符串以子字符串“Hey”开头,因此返回“true”。
Voca中的endsWith函数
当我们想要检查特定文本是否以文本结尾时,使用endsWith()函数。考虑下面所示的示例。
1 2 |
|
它将产生以下输出
1 |
|
这里,我们检查输入字符串是否以子字符串“us?”结尾。它返回“true”,因为输入字符串确实以给定的子字符串结尾。
Voca 中的 include() 函数
当我们想要检查特定文本中是否包含指定文本时,可以使用includes()
函数。考虑下面所示的示例。1 2 |
|
它将产生以下输出
1 |
|
这里,输入字符串包含给定的子字符串“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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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

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
