A JavaScript module for performing various string transformation tasks. To install this module run command
npm i string-transformation
const stringUtil = require('string-transformation');
stringUtil.stringLength(str);
OR
const { stringLength } = require('string-transformation');
Calculates the length of the given string.
Input:
stringLength('Hello, World!');
Output:
13
Convert string to UpperCase.
Input:
convertToUpperCase('hello');
Output:
HELLO
Convert string to LowerCase.
Input:
convertToLowerCase('WORLD');
Output:
world
Capitalize first character of string.
Input:
capitalizeFirstLetter('hello world!');
Output:
Hello world!
Capitalize first character of every word in the string.
Input:
convertToTitleCase('this is a test sentence');
Output:
This Is A Test Sentence
Get a substring from the string.
Input:
getSubstring('abcdef', 1, 4);
Output:
bcd
Trim white space from both end of the string.
Input:
trimWhitespace(' Hello, World! ');
Output:
Hello, World!
Trim leading white space from string.
Input:
trimLeft(' Leading whitespace');
Output:
Leading whitespace
Trim trailing white space from string.
Input:
trimRight('Trailing whitespace ');
Output:
Trailing whitespace
Replace a character in the string.
Input:
replaceCharacter('banana', 'a', 'o');
Output:
bonono
Remove a character from the string.
Input:
removeCharacter('banana', 'a');
Output:
bnn
Split string and gives an array.
Input:
splitString('apple,orange,banana', ',');
Output:
[ 'apple', 'orange', 'banana' ]
Joins an array of string and provides a string.
Input:
joinStrings(['apple', 'orange', 'banana'], ', ');
Output:
apple, orange, banana
Get the first occorrence of a substring in a string.
Input:
firstOccurrence('programming', 'g');
Output:
3
Get all the occorrence of a substring in a string as a array.
Input:
findAllOccurrences('banana', 'a');
Output:
[1, 3, 5]
Replace a substring from the string with a new string.
Input:
replaceSubstring('hello world', 'world', 'there');
Output:
hello there
Remove white space from string and replace it with new character.
Input:
replaceWhitespaceWith('Hello World!', '-');
Output:
Hello----World!
Make the string longer by adding padding character to the left of the string if the string is shorter than the total required length of the string.
Input:
padLeft('42', '0', 5);
Output:
00042
Make the string longer by adding padding character to the right of the string if the string is shorter than the total required length of the string.
Input:
padRight('42', '0', 5);
Output:
42000
Reverse the string.
Input:
reverseString('world');
Output:
dlrow
Check if the provided string is a palindrome.
Input:
isPalindrome('racecar');
Output:
true
Check if the provided string contains only numeric values (numbers).
Input:
isNumeric('42.5');
Output:
true
Encode the provided string to base64.
Input:
encodeBase64('Hello, World!');
Output:
SGVsbG8sIFdvcmxkIQ==
Decode base64 code to string.
Input:
decodeBase64('SGVsbG8sIFdvcmxkIQ==');
Output:
Hello, World!
Returns an array of all occurrences of a given regular expression pattern in a string.
Input:
matchRegex('abc123def456', /\d+/g);
Output:
["123", "456"]
Replaces occurrences of a given regular expression pattern with a specified replacement in a string.
Input:
replaceRegex('abc123def456', /\d+/g, 'X');
Output:
abcXdefX
Encodes a string for safe use in a URL.
Input:
encodeURIComponent('Hello, World!');
Output:
Hello%2C%20World!
Decodes a URL-encoded string back to its original form.
Input:
decodeURIComponent('Hello%2C%20World%21');
Output:
Hello, World!
Extracts the path portion from a given URL.
Input:
getPathFromURL('https://example.com/path/file.html');
Output:
/path/file.html
Compares two strings lexicographically and returns a comparison result.
Input:
compareStrings('apple', 'banana');
Output:
-1
Checks if two strings are equal, ignoring their case.
Input:
equalsIgnoreCase('hello', 'Hello');
Output:
true
Formats a string using placeholders and corresponding values.
Input:
formatString('Hello, {0}!', 'World');
Output:
Hello, World!
Input:
formatString('Hello {0}, Hello {1}!', 'there', 'World');
Output:
Hello there, Hello World!
Retrieves the Unicode code point at a specific position in a string.
Input:
getUnicodeCodePointAt('A', 0);
Output:
65
Converts a string to a sequence of Unicode code points.
Input:
convertToUnicode('Hello');
Output:
U+48 U+65 U+6c U+6c U+6f
Truncates a string to a specified length with an optional ellipsis.
Input:
truncateText('Lorem ipsum dolor sit amet', 15);
Output:
Lorem ipsum dol...
Replaces consecutive whitespace characters with a single space.
Input:
normalizeWhitespace(' Hello World! ');
Output:
Hello World!
Removes all whitespace characters from a string.
Input:
collapseWhitespace('Hello World!');
Output:
HelloWorld!
Determines the type of a character (uppercase, lowercase, digit, etc.).
Input:
getCharacterType('A');
Output:
uppercase
Retrieves the Unicode code point of a character.
Input:
getCharacterCode('A');
Output:
65
Pads a string with a specified content to achieve a desired length.
Input:
padWithContent('Hello', '*', 10);
Output:
**Hello***
Converts a string to camel case format.
Input:
convertToCamelCase('hello-world');
Output:
helloWorld
Converts a string to snake case format.
Input:
convertToSnakeCase('helloWorld');
Output:
hello_world
Wraps a long text string into multiple lines with a specified maximum width.
Input:
wrapText('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 20);
Output:
Lorem ipsum dolor sit
amet, consectetur
adipiscing elit.
Provide uuid for (Universally Unique IDentifier).
Input:
uuid();
Output:
28d921a6-fc82-2dcf79-8244-c7112d52af
Provide uuid which only consist of numbers.
Input:
uuidNumber();
Output:
500879049-52937-13546-19988-1519210004
Provide uuid based on Date and Time.
Input:
uuidDateTime();
Output:
1ee41006916c750-faba78b560-1ee4-9234-0123456789ab