TypeScript String Operations

1.6.1 · active · verified Sun Apr 19

The `typescript-string-operations` library (current stable v1.6.1) provides a collection of lightweight, utility functions for common string manipulations in TypeScript, inspired by C# string operations. It includes robust `formatString` with specifiers (e.g., `{0:L}` for lowercase, `{0:d}` for short date), `joinString` for concatenating arrays or objects with a delimiter, `isNullOrWhiteSpace` for comprehensive empty/null/whitespace checks, and an `emptyString` constant. A `StringBuilder` class is also available for efficient, mutable string construction, mirroring its C# counterpart. The library is designed to be lightweight, has no jQuery dependency, and is unit-tested, making it suitable for modern web applications, including those built with Angular. Its release cadence is feature-driven, with notable breaking changes in version 1.5.0, where uppercase-prefixed methods and the `String` class were deprecated in favor of named function imports to avoid polluting the global `String` object.

Warnings

Install

Imports

Quickstart

Demonstrates installation, importing key functions and the StringBuilder class, and basic usage of `emptyString`, `isNullOrWhiteSpace`, `formatString` (with indexed arguments and object specifiers), `joinString` (with variadic arguments, arrays, and objects), and `StringBuilder` methods.

import { StringBuilder, emptyString, joinString, formatString, isNullOrWhiteSpace } from 'typescript-string-operations';

// 1. Using emptyString
console.log('Empty String:', emptyString); // Output: Empty String:

// 2. Using isNullOrWhiteSpace
const value1 = null;
const value2 = '';
const value3 = '   ';
const value4 = 'Hello';
console.log(`isNullOrWhiteSpace(${JSON.stringify(value1)}):`, isNullOrWhiteSpace(value1)); // Output: true
console.log(`isNullOrWhiteSpace(${JSON.stringify(value2)}):`, isNullOrWhiteSpace(value2)); // Output: true
console.log(`isNullOrWhiteSpace(${JSON.stringify(value3)}):`, isNullOrWhiteSpace(value3)); // Output: true
console.log(`isNullOrWhiteSpace(${JSON.stringify(value4)}):`, isNullOrWhiteSpace(value4)); // Output: false

// 3. Using formatString with indexed arguments
const userId = '2db5da20-1c5d-4f1a-8fd4-b41e34c8c5b5';
const formattedImageName = formatString("image_{0}.jpg", userId);
console.log('Formatted Image Name:', formattedImageName); // Output: image_2db5da20-1c5d-4f1a-8fd4-b41e34c8c5b5.jpg

// 4. Using formatString with specifiers and objects
interface Fruit {
    type: string;
    color: string;
    shippingDate: Date;
    amount: number;
}

const fruit: Fruit = {
    type: "apple",
    color: "RED",
    shippingDate: new Date(2018, 1, 1),
    amount: 10000
};

const fruitDescription = formatString("The {type:U} is {color:L} shipped on {shippingDate:s} with an amount of {amount:n}", fruit);
console.log('Fruit Description:', fruitDescription); // Output: The APPLE is red shipped on 2018-01-01T00:00:00 with an amount of 10.000

// 5. Using joinString
const joinedFruits = joinString("; ", "Apple", "Banana", "Orange");
console.log('Joined Fruits (args):', joinedFruits); // Output: Apple; Banana; Orange

const fruitArray = ['Mango', 'Pineapple'];
const joinedFruitArray = joinString(", ", fruitArray);
console.log('Joined Fruits (array):', joinedFruitArray); // Output: Mango, Pineapple

const objectToJoin = { Name: "Foo", Value: "Bar", ID: "123" };
const joinedObject = joinString('.', objectToJoin);
console.log('Joined Object:', joinedObject); // Output: Foo.Bar.123

// 6. Using StringBuilder
const sb = new StringBuilder();
sb.Append('Hello').Append(' ').AppendLine('World!');
sb.AppendFormat('The answer is {0}.', 42);
console.log('StringBuilder Output:\n', sb.ToString());
/* Expected Output:
Hello World!
The answer is 42.
*/

view raw JSON →