String Utilities refer to a set of functions or tools used for manipulating and working with strings (sequences of characters). These utilities can help perform various tasks such as formatting, cleaning, searching, splitting, and transforming strings in different ways. They are commonly used in programming, text processing, and data manipulation.
Common String Utilities:
Here are some of the most common string utilities and what they can do:
Concatenate: Combine two or more strings into a single string.
Example: "Hello" + " " + "World" → "Hello World"
Substring: Extract a part of a string based on a specified start and end position.
Example: "Hello World".substring(0, 5) → "Hello"
Length: Get the length (number of characters) of a string.
Example: "Hello World".length() → 11
Trim: Remove whitespace from both ends of a string.
Example: " Hello World ".trim() → "Hello World"
Replace: Replace one or more characters within a string.
Example: "Hello World".replace("World", "Everyone") → "Hello Everyone"
Uppercase/Lowercase: Convert all characters in a string to uppercase or lowercase.
Example: "Hello".toUpperCase() → "HELLO"
Example: "WORLD".toLowerCase() → "world"
Split: Split a string into an array or list of substrings based on a separator.
Example: "apple,banana,orange".split(",") → ["apple", "banana", "orange"]
Find: Search for a specific character or substring within a string.
Example: "Hello World".indexOf("World") → 6 (position where "World" starts)
Join: Combine elements of an array or list into a single string, often using a separator.
Example: ["apple", "banana", "orange"].join(", ") → "apple, banana, orange"
Match: Search a string for a pattern (using regular expressions).
Example: "Hello World".match(/o/g) → ["o", "o"]
Escape: Convert special characters into their escaped versions (e.g., " becomes \").
Example: "Hello "World"".escape() → "Hello \"World\""
Unescape: Convert escape sequences (like \" or \\) back into their original characters.
Example: "Hello \"World\"".unescape() → "Hello "World"
Padding: Add padding characters (spaces, zeros, etc.) to the beginning or end of a string to reach a specific length.
Example: "5".padStart(3, "0") → "005"
Example: "5".padEnd(3, "0") → "500"
Character Search: Check if a string contains a specific character or substring.
Example: "Hello".includes("e") → true
Example: "Hello".startsWith("H") → true
Example: "Hello".endsWith("o") → true
Escape HTML: Convert special HTML characters (like <, >, &) to their HTML entities (<, >, &).
Example: "5 > 3".escapeHTML() → "5 > 3"
Reverse String: Reverse the order of characters in a string.
Example: "Hello".split("").reverse().join("") → "olleH"
Check Palindrome: Check if a string reads the same forwards and backwards.
Example: "madam".isPalindrome() → true
Remove Non-Alphanumeric Characters: Strip out non-alphanumeric characters (e.g., punctuation, symbols).
Example: "Hello, World!".replace(/[^a-zA-Z0-9]/g, "") → "HelloWorld"