You can remove the first occurrence of a string by using different methods. One of the most effective ways of removing the first occurrence from a string is by converting and splitting the string into an array, then you can use the shift() function to remove the very first character from the string.

For example...

//remove first occurrence by converting to array
let school = "University of New Hampshire";
let arr = school.split(' ');//split into array
arr.shift();//remove first character
school = arr.join(' ');//convert into string
console.log(school);

When you log the variable ‘school’ into the console, you will see that the first character ‘University’ has been removed from the string and the output still remains a string and not an array.

PS: shift() function only removes the first character from a string

Another method of removing the first occurrence from a string is by using the substring() method. This method removes the first letter from a string. Although it works with index numbers, it is also effective in removing the first letter that occurs in a string.

For example...

let team = "football one";
var team1 = team.substring(1);
console.log(team1);

When you log the variable ‘team1’ into the console, you will observe that ‘f’ from the word ‘football’ has been removed. If you set the substring to 2, ‘f’ and the next letter to it will be removed and you will get something like ‘otball one’ in your console.

Another effective way is to use the replace() method. Two arguments are passed into the replace method in strings. The first argument would contain the letter/word you want to remove from the string, and the second argument will contain an empty string.

For example:

let name = "Jaack Hardy";
name = name.replace("a", "");
console.log(name);

When you log the variable ‘name’ to the console, you will see that the first occurrence of ‘a has been replaced/removed.

You can also use forward slashes to encapsulate the first argument.

For example...

let greetings = "Hello from New Jersey!";
console.log(greetings);
greetings = greetings.replace(/Hello/, "");
console.log(greetings);

You can also remove the first occurrence from a string by using the slice() method. The slice method also works with two arguments but index numbers. Using the slice() method, it gets two arguments, the starting index and ending index.

let strin = "How To Remove A String";
console.log(strin);
strin = strin.slice(3, 99999999999);
console.log(strin);

Conclusion:

We have discussed four different methods which you can use to remove the first instance of a character/letter/word from a string. Make sure to use either method as applicable to your code to avoid output errors.