Back to Exercises
Strings (Real Patterns)
20 Exercises Available
trim, normalize, search, slug — commonly used in UI/content.
1.Medium
Slugify (URL-friendly)
Create a function `slugify(text)`. Rules: lowercase, trim, spaces → dash, remove non-alphanumeric, collapse multiple dashes.
Sample Output:
slugify("Hello World!") => "hello-world"
slugify(" My First Post ") => "my-first-post"View Solution
2.Easy
Mask Phone Number
Function `maskPhone(phone)` banao jo last 4 digits visible रखे, बाकी `*`.
Example: "9876543210" → "******3210"
Sample Output:
maskPhone("9876543210") => "******3210"
maskPhone("+91-98765-43210") => "******3210"View Solution
3.Easy
Count Words (robust)
Create a function `wordCount(text)` that handles multiple spaces/newlines and returns word count.
Sample Output:
wordCount("Hello World") => 2
wordCount(" Multiple Spaces ") => 2
wordCount("") => 0View Solution
4.Easy
Case-insensitive Includes
Create a function `includesCI(text, query)` (case-insensitive).
Sample Output:
includesCI("Hello World", "hello") => true
includesCI("JavaScript", "SCRIPT") => true
includesCI("Python", "java") => falseView Solution
5.Medium
Extract Domain from URL
Create a function `getDomain(url)`. Return null for invalid URL.
Sample Output:
getDomain("https://google.com/search") => "google.com"
getDomain("http://example.com") => "example.com"
getDomain("invalid-url") => nullView Solution
6.Easy
Capitalize First Letter of Each Word
Create a function `titleCase(str)` that capitalizes first letter of each word in string.
Example: titleCase("hello world") => "Hello World"
Sample Output:
titleCase("hello world") => "Hello World"
titleCase("JAVASCRIPT IS FUN") => "Javascript Is Fun"View Solution
7.Easy
Remove All Whitespace
Function `removeWhitespace(str)` banao jo string se sabhi whitespace characters remove kare.
Example: removeWhitespace("hello world") => "helloworld"
Sample Output:
removeWhitespace("hello world") => "helloworld"
removeWhitespace(" a b c ") => "abc"View Solution
8.Easy
Check if String Ends with Suffix
Function `endsWith(str, suffix)` banao jo check kare ki string suffix se end hoti hai ya nahi (case-insensitive).
Example: endsWith("hello.js", ".js") => true
Sample Output:
endsWith("hello.js", ".js") => true
endsWith("file.TXT", ".txt") => true
endsWith("test", ".js") => falseView Solution
9.Easy
Extract Numbers from String
Function `extractNumbers(str)` banao jo string se sabhi numbers extract kare.
Example: extractNumbers("abc123def456") => [123, 456]
Sample Output:
extractNumbers("abc123def456") => [123, 456]
extractNumbers("price: $100") => [100]View Solution
10.Easy
Reverse String (preserve spaces)
Function `reverseString(str)` banao jo string ko reverse kare but spaces ko preserve kare.
Example: reverseString("hello world") => "dlrow olleh"
Sample Output:
reverseString("hello") => "olleh"
reverseString("hello world") => "dlrow olleh"View Solution
11.Easy
Check if String Contains Only Letters
Function `isAlpha(str)` banao jo check kare ki string me sirf letters hain (a-z, A-Z).
Example: isAlpha("Hello") => true, isAlpha("Hello123") => false
Sample Output:
isAlpha("Hello") => true
isAlpha("Hello123") => false
isAlpha("") => falseView Solution
12.Easy
Truncate String with Ellipsis
Function `truncate(str, maxLength)` banao jo string ko truncate kare aur ellipsis add kare.
Example: truncate("Hello world", 8) => "Hello..."
Sample Output:
truncate("Hello world", 8) => "Hello..."
truncate("Short", 10) => "Short"View Solution
13.Easy
Count Occurrences of Substring
Function `countOccurrences(str, substr)` banao jo string me substring ki count return kare.
Example: countOccurrences("hello hello", "hello") => 2
Sample Output:
countOccurrences("hello hello", "hello") => 2
countOccurrences("aaa", "aa") => 2View Solution
14.Easy
Remove Duplicate Characters
Function `removeDuplicates(str)` banao jo string se duplicate characters remove kare.
Example: removeDuplicates("hello") => "helo"
Sample Output:
removeDuplicates("hello") => "helo"
removeDuplicates("aabbcc") => "abc"View Solution
15.Medium
Check if String is Valid URL
Function `isValidURL(str)` banao jo check kare ki string valid URL hai ya nahi.
Example: isValidURL("https://example.com") => true
Sample Output:
isValidURL("https://example.com") => true
isValidURL("not-a-url") => falseView Solution
16.Easy
Pad String to Fixed Width
Function `pad(str, width, char=" ", side="right")` banao jo string ko specified width tak pad kare.
Example: pad("hello", 10, " ", "right") => "hello "
Sample Output:
pad("hello", 10) => "hello "
pad("hello", 10, " ", "left") => " hello"View Solution
17.Medium
Find Longest Common Prefix
Function `longestCommonPrefix(strs)` banao jo array of strings me longest common prefix find kare.
Example: longestCommonPrefix(["flower", "flow", "flight"]) => "fl"
Sample Output:
longestCommonPrefix(["flower", "flow", "flight"]) => "fl" longestCommonPrefix(["dog", "cat"]) => ""
View Solution
18.Medium
Convert CamelCase to kebab-case
Function `camelToKebab(str)` banao jo camelCase string ko kebab-case me convert kare.
Example: camelToKebab("helloWorld") => "hello-world"
Sample Output:
camelToKebab("helloWorld") => "hello-world"
camelToKebab("myVariableName") => "my-variable-name"View Solution
19.Easy
Check if String is Anagram
Function `isAnagram(str1, str2)` banao jo check kare ki dono strings anagram hain ya nahi.
Example: isAnagram("listen", "silent") => true
Sample Output:
isAnagram("listen", "silent") => true
isAnagram("hello", "world") => falseView Solution
20.Medium
Extract Query Parameters from URL
Function `getQueryParams(url)` banao jo URL se query parameters extract kare.
Example: getQueryParams("https://example.com?name=John&age=30") => {name: "John", age: "30"}
Sample Output:
getQueryParams("https://example.com?name=John&age=30") => {name: "John", age: "30"}
getQueryParams("https://example.com") => {}View Solution