programming-examples/js/String/Remove the first occurrence of a given 'search string' from a string.js

9 lines
335 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function remove_first_occurrence(str, searchstr) {
var index = str.indexOf(searchstr);
if (index === -1) {
return str;
}
return str.slice(0, index) + str.slice(index + searchstr.length);
}
console.log(remove_first_occurrence("The quick brown fox jumps over the lazy dog", 'the'));