How to remove portion of a string after certain character in JavaScript ?

Given a URL and the task is to remove a portion of URL after a certain character using JavaScript.

  • split() method: This method is used to split a string into an array of substrings, and returns the new array.

    Syntax:

    string.split(separator, limit)

    Parameters:

    • separator: It is optional parameter. It specifies the character, or the regular expression, to use for splitting the string. If not used, the whole string will be returned (an array with only one item).
    • limit: It is optional parameter. It specifies the integer that specifies the number of splits, items beyond the split limit will be excluded from the array.

    Return value: Returns a new array, having the splitted items.

  • JavaScript String substring() Method: This method gets the characters from a string, between two defined indices, and returns the new sub string. This method gets the characters in a string between “start” and “end”, excluding “end” itself.

    Syntax:

    string.substring(start, end)

    Parameters:

    • start: It is required parameter. It specifies the position from where to start the extraction. Index of first character starts from 0.
    • end: It is optional parameter. It specifies the position (eccluding) where to stop the extraction. If not used, it extracts the whole string.

    Return value: It returns a new string containing the extracted characters.

Example 1: This example uses the substring() method to remove the portion of the string after certain character (?).

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
    <head
        <title
            Remove portion of string after
            certain characters
        </title>
    </head
      
    <body style = "text-align:center;"
      
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
  
        <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;">
        </p>
          
        <button onclick = "GFG_click()">
            click to remove
        </button>
          
        <p id = "GFG_DOWN" style
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            var s = '/path/action?id=11612&value=44944';
            el_up.innerHTML = s;         
              
            function GFG_click() {
                s = s.substring(0, s.indexOf('?'));
                el_down.innerHTML = "String = "+s;
            }         
        </script
    </body
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example 2: This example uses the split() method to remove the portion of the string after certain character (?).

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
    <head
        <title
            Remove portion of string after 
            certain character
        </title>
    </head
      
    <body style = "text-align:center;"
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
  
        <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;">
        </p>
          
        <button onclick = "GFG_click()">
            click to remove
        </button>
          
        <p id = "GFG_DOWN" style
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            var s = '/path/action?id=11612&value=44944';
            el_up.innerHTML = s;         
              
            function GFG_click() {
                s = s.split('?')[0]
                el_down.innerHTML = "String = " + s;
            }         
        </script
    </body
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.