A JavaScript library for emulating Python's string methods.
- Project Description
- Motivation
- Installation
- Usage
- Examples
- License
- Code of Conduct
- Learn More
- Contributing
- Contact
- Happy Coding!
The String Methods Library is a JavaScript library that provides a collection of useful string manipulation functions. It is designed to simplify common string operations and make your coding tasks more efficient.
The motivation behind this project is to create a versatile and easy-to-use library for string manipulation in JavaScript. By providing a range of string methods, this library aims to save developers time and effort when working with strings.
You can install the library using npm:
npm install node-string-methods
To use the library, import the desired modules in your JavaScript code. Here are some examples of the available modules:
Description: Capitalizes the first letter of a string.
Usage:
const { capitalize } = require('node-string-methods');
const str = 'hello, world!';
const capitalized = capitalize(str);
console.log(capitalized); // Output: 'Hello, world!'
Description: Converts a string to its case-folded form (Unicode normalization).
Usage:
const { caseFold } = require('node-string-methods');
const str = 'lEtS CaSeFoLd';
const caseFolded = caseFold(str);
console.log(caseFolded); // Output: 'lets casefold'
Description: Centers a string within a specified width, padding with a character.
Usage:
const { center } = require('node-string-methods');
const str = 'centered';
const centeredStr = center(str, 12, '-');
console.log(centeredStr); // Output: '--centered--'
Description: Counts the occurrences of a substring in a string.
Usage:
const { count } = require('node-string-methods');
const str = 'hello, hello, world!';
const substring = 'hello';
const occurrences = count(str, substring);
console.log(occurrences); // Output: 2
Description: Encodes special characters in a string for HTML.
Usage:
const { encode } = require('node-string-methods');
const str = '<div>Hello, world!</div>';
const encodedStr = encode(str);
console.log(encodedStr); // Output: '<div>Hello, world!</div>'
Description: Checks if a string ends with a specified suffix.
Usage:
const { endsWith } = require('node-string-methods');
const str = 'Hello, world!';
const suffix = 'world!';
const result = endsWith(str, suffix);
console.log(result); // Output: true
Description: Expands tabs in a string to spaces with a specified tab width.
Usage:
const { expandTabs } = require('node-string-methods');
const str = 'Hello\tworld!\t\tThis is a test.';
const tabWidth = 4;
const expandedStr = expandTabs(str, tabWidth);
console.log(expandedStr);
/* Output:
Hello world! This is a test.
*/
Description: Finds the first occurrence of a substring in a string and returns its index.
Usage:
const { find } = require('node-string-methods');
const str = 'Hello, world!';
const substring = 'world';
const index = find(str, substring);
console.log(index); // Output: 7
Description: Formats a string by replacing placeholders with values.
Usage:
const { format } = require('node-string-methods');
const template = 'Hello, {name}! You are {age} years old.';
const data = { name: 'John', age: 30 };
const formattedStr = format(template, data);
console.log(formattedStr); // Output: 'Hello, John! You are 30 years old.'
Description: Checks if a string contains only alphanumeric characters.
Usage:
const { isAlnum } = require('node-string-methods');
const str = 'Hello123';
const result = isAlnum(str);
console.log(result); // Output: true
Description: Checks if a string contains only alphabetic characters.
Usage:
const { isAlpha } = require('node-string-methods');
const str = 'Hello';
const result = isAlpha(str);
console.log(result); // Output: true
Description: Checks if a string contains only ASCII characters.
Usage:
const { isAscii } = require('node-string-methods');
const str = 'Hello, world!';
const result = isAscii(str);
console.log(result); // Output: true
Description: Checks if a string represents a decimal number.
Usage:
const { isDecimal } = require('node-string-methods');
const str = '42.123';
const result = isDecimal(str);
console.log(result); // Output: true
Description: Checks if a string contains only digit characters.
Usage:
const { isDigit } = require('node-string-methods');
const str = '12345';
const result = isDigit(str);
console.log(result); // Output: true
Description: Checks if a string is a valid identifier.
Usage:
const { isIdentifier } = require('node-string-methods');
const identifier = 'myVariable123';
const result = isIdentifier(identifier);
console.log(result); // Output: true
Description: Checks if a string contains only lowercase alphabetic characters.
Usage:
const { isLower } = require('node-string-methods');
const str = 'hello';
const result = isLower(str);
console.log(result); // Output: true
Description: Checks if a string contains only numeric characters.
Usage:
const { isNumeric } = require('node-string-methods');
const str = '12345';
const result = isNumeric(str);
console.log(result); // Output: true
Description: Checks if a string contains only whitespace characters.
Usage:
const { isSpace } = require('node-string-methods');
const str = ' \t ';
const result = isSpace(str);
console.log(result); // Output: true
Description: Checks if a string is in title case.
Usage:
const { isTitle } = require('node-string-methods');
const str = 'This Is Title Case';
const result = isTitle(str);
console.log(result); // Output: true
Description: Checks if a string contains only uppercase alphabetic characters.
Usage:
const { isUpper } = require('node-string-methods');
const str = 'HELLO';
const result = isUpper(str);
console.log(result); // Output: true
Description: Joins an array of strings into a single string with a specified separator.
Usage:
const { join } = require('node-string-methods');
const arr = ['apple', 'banana', 'cherry'];
const separator = ', ';
const result = join(arr, separator);
console.log(result); // Output: 'apple, banana, cherry'
Description: Left-justifies a string within a specified width, padding with a character.
Usage:
const { leftJustify } = require('node-string-methods');
const str = 'left';
const width = 10;
const paddingChar = '-';
const result = leftJustify(str, width, paddingChar);
console.log(result); // Output: 'le'
Description: Converts a string to lowercase.
Usage:
const { lower } = require('node-string-methods');
const str = 'Hello, World!';
const result = lower(str);
console.log(result); // Output: 'hello, world!'
Description: Removes leading whitespace characters from a string.
Usage:
const { leftStrip } = require('node-string-methods');
const str = ' Hello, World!';
const result = leftStrip(str);
console.log(result); // Output: 'Hello, World!'
Description: Translates characters in a string based on a translation table.
Usage:
const { makeTrans } = require('node-string-methods');
const str = 'Hello, World!';
const translationTable = { 'H': 'h', 'W': 'w' };
const result = makeTrans(str, translationTable);
console.log(result); // Output: 'hello, world!'
Description: Splits a string into three parts based on a separator.
Usage:
const { partition } = require('node-string-methods');
const str = 'apple,banana,cherry';
const separator = ',';
const result = partition(str, separator);
console.log(result); // Output: ['apple', ',', 'banana,cherry']
Description: Replaces all occurrences of a substring in a string.
Usage:
const { replace } = require('node-string-methods');
const str = 'Hello, World!';
const substring = 'World';
const replacement = 'Universe';
const result = replace(str, substring, replacement);
console.log(result); // Output: 'Hello, Universe!'
Description: Finds the last occurrence of a substring in a string.
Usage:
const { rightFind } = require('node-string-methods');
const str = 'apple, banana, cherry, apple';
const substring = 'apple';
const result = rightFind(str, substring);
console.log(result); // Output: 21
Description: Returns the index of the last occurrence of a substring in a string.
Usage:
const { rightIndex } = require('node-string-methods');
const str = 'apple, banana, cherry, apple';
const substring = 'apple';
const result = rightIndex(str, substring);
console.log(result); // Output: 21
Description: Right-justifies a string within a specified width, padding with a character.
Usage:
const { rightJustify } = require('node-string-methods');
const str = 'right';
const width = 10;
const paddingChar = '-';
const result = rightJustify(str, width, paddingChar);
console.log(result); // Output:--right'
Description: Splits a string into three parts based on a separator from the right.
Usage:
const { rightPartition } = require('node-string-methods');
const str = 'apple,banana,cherry';
const separator = ',';
const result = rightPartition(str, separator);
console.log(result); // Output: ['apple,banana', ',', 'cherry']
Description: Removes trailing whitespace characters from a string.
Usage:
const { rightStrip } = require('node-string-methods');
const str = 'Hello, World! ';
const result = rightStrip(str);
console.log(result); // Output: 'Hello, World!'
Description: Splits a string into an array of substrings based on a separator.
Usage:
const { split } = require('node-string-methods');
const str = 'apple,banana,cherry';
const separator = ',';
const result = split(str, separator);
console.log(result); // Output: ['apple', 'banana', 'cherry']
Description: Splits a multi-line string into an array of lines.
Usage:
const { splitLines } = require('node-string-methods');
const str = 'Line 1\nLine 2\nLine 3';
const result = splitLines(str);
console.log(result); // Output: ['Line 1', 'Line 2', 'Line 3']
Description: Checks if a string starts with a specified prefix.
Usage:
const { startsWith } = require('node-string-methods');
const str = 'Hello, World!';
const prefix = 'Hello';
const result = startsWith(str, prefix);
console.log(result); // Output: true
Description: Removes leading and trailing whitespace characters from a string.
Usage:
const { strip } = require('node-string-methods');
const str = ' Hello, World! ';
const result = strip(str);
console.log(result); // Output: 'Hello, World!'
Description: Swaps the case (upper to lower and vice versa) of characters in a string.
Usage:
const { swapCase } = require('node-string-methods');
const str = 'Hello, World!';
const result = swapCase(str);
console.log(result); // Output: 'hELLO, wORLD!'
Description: Converts a string to title case (capitalizes the first letter of each word).
Usage:
const { title } = require('node-string-methods');
const str = 'hello world';
const result = title(str);
console.log(result); // Output: 'Hello World'
Description: Converts a string to uppercase.
Usage:
const { upper } = require('node-string-methods');
const str = 'Hello, World!';
const result = upper(str);
console.log(result); // Output: 'HELLO, WORLD!'
Description: Pads a number with leading zeros to a specified width.
Usage:
const { zeroFill } = require('node-string-methods');
const num = 42;
const width = 5;
const result = zeroFill(num, width);
console.log(result); // Output: '00042'
For more examples and detailed documentation, please refer to the docs directory.
This project is licensed under the MIT License.
Please read our Code of Conduct to understand the behavior expected in our community.
Explore our Learn.md file to find additional resources for learning and improving your skills.
We welcome contributions! See our Contributing Guide for more details on how to get started.
For any questions or feedback, feel free to contact me:
- Name: Pabitra Banerjee
- Email: Pabitra Banerjee
- GitHub: PB2204
We hope you find the String Methods Library useful in your projects. Happy coding!