Nexium

Strings

Unlock the Power of Strings with Nexium's Essential Methods

Import the NString

const { NString } = require('nexium')

trimStart

Removes whitespace from the beginning of a string.

const trimedStart = NString.trimStart('    Hello World')

trimEnd

Removes whitespace from the end of a string.

const trimedEnd = NString.trimEnd('Hello World    ')

isPalindrome

Checks if a string reads the same forwards and backwards.

const isPalindrome = NString.isPalindrome('madam') // true

generate random string

Generates a random string of specified length using given pattern characters.

const length = 15
const pattern = 'abc123'
const randomString = NString.generateRandomString(length, pattern)

remove diacritics

Removes accent marks and diacritical signs from characters.

const removedDiacritics = NString.replaceDiacritics('Café')
console.log('removedDiacritics -> ', removedDiacritics)

replace string

Replaces all occurrences of a substring with another string.

const replacedString = NString.replaceString('hello world', 'world', 'there')

endsWith

Checks if a string ends with the specified substring.

const endsWith = NString.endsWith('hello world', 'world')

case convertors

Converts strings between different case formats (kebab, snake, camel).

const kebab = NString.toCase('hello world', 'kebab-case')
 
const snakeToCamel = NString.toCase('snake_case_test', 'snake-to-camel')
 
const camelToSnake = NString.toCase('camelCaseTest', 'camel-to-snake')

slugify

Converts a string into a URL-friendly slug format.

const slugified = NString.slugify('Special!!  Characters---and Spaces', true)

wordsArray

Splits a string into an array of words, removing extra spaces.

const wordsArray = NString.getWordsArray('   Multiple    spaces   here ')

insertedStringAt

Inserts a string at a specified position within another string.

const insertedStringAt = NString.insertStringAt('abcdef', '123', 3)

removeDuplicates

Removes consecutive duplicate characters from a string.

const removeDuplicates = NString.removeDuplicates('aabbcc')

stripHTML

Removes HTML tags from a string, leaving only the text content.

const stripedHTML = NString.stripHTML('<p>Hello, <b>world</b>!</p>')

reverse

Reverses the characters in a string.

const reversedString = NString.reverse('hello')

countOccurrences

Counts how many times a substring appears in a string.

const countOccurrences = NString.countOccurrences('hello world hello', 'hello')

On this page