-
Notifications
You must be signed in to change notification settings - Fork 0
String Operations
Phantom.js provides 28 string operations for manipulation, transformation, and validation.
- Basic Operations
- Transformation Operations
- Search and Check Operations
- Padding Operations
- Trimming Operations
- Advanced Operations
Check if a string contains a substring.
phantom.strings.operation.find("hello world", "world");
// Output: true
phantom.strings.operation.find("hello world", "xyz");
// Output: falseAlias for find - check if string contains substring.
phantom.strings.operation.contains("hello world", "world");
// Output: trueGet the length of a string.
phantom.strings.operation.length("hello");
// Output: 5Check if string is empty.
phantom.strings.operation.isEmpty("");
// Output: true
phantom.strings.operation.isEmpty("hello");
// Output: falseCheck if string is empty or only whitespace.
phantom.strings.operation.isBlank(" ");
// Output: true
phantom.strings.operation.isBlank("hello");
// Output: falseConvert string to uppercase.
phantom.strings.operation.toUpperCase("hello");
// Output: "HELLO"Convert string to lowercase.
phantom.strings.operation.toLowerCase("HELLO");
// Output: "hello"Capitalize first letter, lowercase the rest.
phantom.strings.operation.capitalize("hello");
// Output: "Hello"
phantom.strings.operation.capitalize("HELLO");
// Output: "Hello"Reverse a string.
phantom.strings.operation.reverse("hello");
// Output: "olleh"Check if string starts with prefix.
phantom.strings.operation.startsWith("hello", "he");
// Output: trueCheck if string ends with suffix.
phantom.strings.operation.endsWith("hello", "lo");
// Output: truePad string on the left side.
phantom.strings.operation.leftPad("test", "0", 3);
// Output: "000test"
phantom.strings.operation.leftPad("test", undefined, 3);
// Output: " test" (defaults to space)Pad string on the right side.
phantom.strings.operation.rightPad("test", "0", 3);
// Output: "test000"Pad string on both sides.
phantom.strings.operation.dualPad("test", "0", 2);
// Output: "00test00"Remove whitespace from the left side.
phantom.strings.operation.leftTrim(" test");
// Output: "test"Remove whitespace from the right side.
phantom.strings.operation.rightTrim("test ");
// Output: "test"Remove whitespace from both sides.
phantom.strings.operation.trim(" test ");
// Output: "test"Replace first occurrence of a substring.
phantom.strings.operation.replace("hello world", "world", "universe");
// Output: "hello universe"
phantom.strings.operation.replace("hello hello", "hello", "hi");
// Output: "hi hello" (only first occurrence)Replace all occurrences of a substring.
phantom.strings.operation.replaceAll("hello hello", "hello", "hi");
// Output: "hi hi"
// Handles special regex characters safely
phantom.strings.operation.replaceAll("a.b.c", ".", "-");
// Output: "a-b-c"Split string by delimiter.
phantom.strings.operation.split("a,b,c", ",");
// Output: ["a", "b", "c"]Extract a substring.
phantom.strings.operation.substring("hello world", 0, 5);
// Output: "hello"
phantom.strings.operation.substring("hello world", 6);
// Output: "world" (from index 6 to end)Insert/delete characters in a string.
phantom.strings.operation.splice("hello world", 5, 1, "X");
// Output: "helloXworld" (replaces space with X)
phantom.strings.operation.splice("hello", 2, 0, "XX");
// Output: "heXXllo" (inserts without deleting)Compare two strings lexicographically.
phantom.strings.operation.compare("test", "test");
// Output: 0 (equal)
phantom.strings.operation.compare("zebra", "apple");
// Output: 1 (first > second)
phantom.strings.operation.compare("apple", "zebra");
// Output: -1 (first < second)Join two strings with a delimiter.
phantom.strings.operation.join("hello", "world", " ");
// Output: "hello world"Repeat a string N times.
phantom.strings.operation.repeat("a", 3);
// Output: "aaa"Remove all occurrences of a substring.
phantom.strings.operation.remove("hello world", "l");
// Output: "heo word"Using Chaining (Recommended - v0.1.5-BETA+):
var raw = phantom.maps.channel.get("rawData");
// Clean and normalize using chaining
var cleanedData = phantom.strings.chain(raw)
.trim()
.toLowerCase()
.capitalize()
.value();
phantom.maps.channel.save("cleanedData", cleanedData);Traditional Way (Still Works):
var raw = phantom.maps.channel.get("rawData");
// Clean and normalize
var step1 = phantom.strings.operation.trim(raw);
var step2 = phantom.strings.operation.toLowerCase(step1);
var step3 = phantom.strings.operation.capitalize(step2);
phantom.maps.channel.save("cleanedData", step3);var email = phantom.maps.channel.get("email");
if (!phantom.strings.operation.isEmpty(email) &&
phantom.strings.operation.contains(email, "@")) {
// Valid email format
phantom.maps.channel.save("validEmail", email);
}var id = phantom.maps.channel.get("id");
// Pad with zeros
var paddedId = phantom.strings.operation.leftPad(id, "0", 8);
// Output: "00001234"
// Format as display string
var displayId = "ID-" + paddedId;var fullName = "John, Doe";
// Split and process
var parts = phantom.strings.operation.split(fullName, ", ");
var firstName = phantom.strings.operation.trim(parts[0]);
var lastName = phantom.strings.operation.trim(parts[1]);- Always handle null/undefined - Most operations convert them to empty strings
- Use trim before processing - Clean input data first
- Chain operations - Build transformation pipelines
- Validate before processing - Check isEmpty/isBlank when needed
- Number Operations - Work with numeric data
- Map Operations - Store and retrieve string data
- Examples - Real-world string processing examples
Phantom - A product of David Labs.