w3resource

JavaScript: Get a part of string after a specified character

JavaScript String: Exercise-22 with Solution

Write a JavaScript function to get a part of string after a specified character.

Test Data:
console.log(subStrAfterChars('w3resource: JavaScript Exercises', ':','a'));
console.log(subStrAfterChars('w3resource: JavaScript Exercises', 'E','b'));
Output:
"w3resource"
"xercises"

Sample Solution:

JavaScript Code:

// Define a function named subStrAfterChars with parameters str (input string), char (character to search for), and pos (position indicator).
function subStrAfterChars(str, char, pos)
{
  // If the position indicator is 'b' (before), return the substring after the specified character.
  if(pos=='b')
    return str.substring(str.indexOf(char) + 1);
  // If the position indicator is 'a' (after), return the substring before the specified character.
  else if(pos=='a') 
    return str.substring(0, str.indexOf(char));
  // If the position indicator is neither 'a' nor 'b', return the original string.
  else
    return str;  
}

// Test the subStrAfterChars function by printing the substring after ':' with the position indicator 'a'.
console.log(subStrAfterChars('w3resource: JavaScript Exercises', ':','a'));

// Test the subStrAfterChars function by printing the substring before 'E' with the position indicator 'b'.
console.log(subStrAfterChars('w3resource: JavaScript Exercises', 'E','b'));

Output:

w3resource
xercises

Explanation:

In the exercise above,

  • The "subStrAfterChars()" function takes three parameters: 'str' (the input string), 'char' (the character to search for), and 'pos' (the position indicator).
  • Inside the function, it checks the value of the 'pos 'parameter:
    • If 'pos' is 'b' (before), it returns the substring after the specified character using 'substring' and 'indexOf' functions.
    • If 'pos' is 'a' (after), it returns the substring before the specified character.
    • If 'pos' is neither 'a' nor 'b', it returns the original string.
  • The function is tested with two examples using console.log, demonstrating how to extract substrings before and after specific characters.

Flowchart:

Flowchart: JavaScript- Get a part of string after a specified character

Live Demo:

See the Pen JavaScript Get a part of string after a specified character - string-ex-22 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.

Previous: Write a JavaScript function to repeat a string a specified times.
Next: Write a JavaScript function to strip leading and trailing spaces from a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript-exercises/javascript-string-exercise-22.php