From c8277e6f774a33492628bfe4af7d14ef52af5c53 Mon Sep 17 00:00:00 2001 From: Isabella Hochschild <40366749+isabellahoch@users.noreply.github.com> Date: Mon, 25 Nov 2024 21:23:32 -0800 Subject: [PATCH] Create stringStuff.ts --- stringStuff.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 stringStuff.ts diff --git a/stringStuff.ts b/stringStuff.ts new file mode 100644 index 0000000..29c2081 --- /dev/null +++ b/stringStuff.ts @@ -0,0 +1,30 @@ +// Function to check if a string is empty +export function isEmptyString(str: string): boolean { + return str.trim() === ''; +} + +// Function to check if a string is a palindrome +export function isPalindrome(str: string): boolean { + const reversedStr = str.split('').reverse().join(''); + return str === reversedStr; +} + +// Function to count the number of occurrences of a substring in a string +export function countSubstringOccurrences(str: string, substring: string): number { + const regex = new RegExp(substring, 'g'); + const matches = str.match(regex); + return matches ? matches.length : 0; +} + +// Function to truncate a string to a specified length +export function truncateString(str: string, maxLength: number): string { + if (str.length <= maxLength) { + return str; + } + return str.slice(0, maxLength) + '...'; +} + +// Function to capitalize the first letter of each word in a string +export function capitalizeWords(str: string): string { + return str.split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' '); +}