Warning: session_start(): open(/tmp/sess_a4157f1be0b25d65dbdeced177bd0ad1, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Remove Substring From String In JavaScript - LearnShareIT

How To Remove Substring From String In JavaScript

Remove Substring From String in JavaScript

Javascript has support for a lot of string handling methods, including methods for replacing strings or cutting strings. This tutorial will help you use those methods and for loops to remove substring from string in JavaScript at your disposal.

Solutions to Remove Substring From String in JavaScript

Method 1: With for loops

This is the simplest and most flexible way to remove substring from string in JavaScript. Every string handling method in Javascript has the application of for loops.

The most important part of this method is to find the condition for the for loop to stop and perform the assignment of the remaining values ​​through a new variable.

For example, I have a string my_name, and I want to trim to get the first six letters, then assign it to the new_name string. I do as follows:

var my_name ='Edward James';
var new_name = '';

for (const key in my_name) {
    if(key < 6) {
        new_name = new_name + my_name[key];
    }
}

console.log('The first six letters of my name are:' + new_name)

Output:

The first six letters of my name are: Edward

Depending on your rules, you can manipulate strings in many different ways. I highly recommend this way because it helps to understand how the built-in methods work.

Another example:

var my_string ='hello everyone, I am Edward';
var new_string = '';

for (const key in my_string) {
    if(key > 15) {
        new_string = new_string + my_string[key];
    }
}

console.log(new_string)

Output: 

I am Edward

Method 2: String.replace()

This is a built-in method in Javascript. It is very powerful in string manipulation and is significantly used because of its convenience. Here I will give the syntax, parameters, some examples and compare it with for loops.

Syntax:

string.replace(searchvalue, newvalue)

Parameter:

  • Searchvalue: The value to be searched for in the string.
  • Newvalue: The value will be substituted for the searchvalue in the new string returned by the method.

Example:

var my_name ='Edward James';
var new_name = my_name.replace('Edward', 'Thomas')
console.log('My new name is: ' + new_name)

Output:

My new name is: Thomas James

Here we can see that Thomas has replaced Edward. In addition, it can also replace all the substrings found in the string by formatting the string as follows:

Example:

var sayHi ='hi A, hi B, hi C';
var sayHello = sayHi.replace(/hi/g, 'hello')
console.log(sayHello)

Output:

hello A, hello B, hello C

String.replace() is a very versatile method in Javascript. It has many critical applications in string processing. If you know this method, you can process your strings efficiently.

Summary

As for coming up with a rule for using a for loop, it is not easy compared to Javascript built-in string.replace() method. However, more flexibility in string handling can be seen when using for loops. In the end, all support methods are geared toward ease of use, so you should choose the right way to remove substring from string in JavaScript.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *