LearnFrenzy
Sign in Take a free test

String Methods · Exercise 4 of 4

String Methods String Methods Coding Problems

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Text-processing programs with string methods and regular expressions: slugs, sentence case, counting substrings, validating Indian mobile numbers and PAN, extracting hashtags, a template engine, case conversion, smart truncation, query strings and search highlighting. Write each one yourself first, then open the solution – every solution was run on Node.js and the output shown is the real output.

Question 1 Easy

Convert article titles into URL slugs: lower case, words joined by single hyphens, no punctuation and no hyphens at the ends.

Question page & discussion
Question 2 Medium

Convert a block of text written in random case to sentence case: everything lower-case except the first letter of each sentence (after ., ! or ?).

Question page & discussion
Question 3 Medium

Count how many times "ana" occurs in "bananarama" – once without overlaps (split / match) and once allowing overlaps (indexOf loop).

Question page & discussion
Question 4 Medium

Validate 10-digit Indian mobile numbers (starting with 6–9, optional +91 and spaces) and PAN numbers (5 letters, 4 digits, 1 letter).

Question page & discussion
Question 5 Medium

From a social media post, list the unique hashtags (lower-cased) and the mentioned users with the position of each mention.

Question page & discussion
Question 6 Hard

Write render(template, data) that replaces {{ key }} and {{ key.nested }} placeholders (spaces allowed) with values from data, leaving unknown placeholders visible as [missing:key].

Question page & discussion
Question 7 Medium

Write toSnake("userFirstName") → "user_first_name" and toCamel("user_first_name") → "userFirstName", and convert all keys of an API object.

Question page & discussion
Question 8 Easy

Write truncate(text, max) that shortens text to at most max characters without cutting a word, and adds "…" only when something was removed.

Question page & discussion
Question 9 Medium

Parse "?q=java%20script&page=2&tag=js&tag=css&debug" into an object (decode values, repeated keys become arrays, keys without values become true), then compare with URLSearchParams.

Question page & discussion
Question 10 Hard

Wrap every case-insensitive occurrence of a search term in <mark> tags, keeping the original case of the text, and make it safe for terms such as "c++" that contain regex characters.

Question page & discussion
All exercises doneBack to String MethodsRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links