How to remove the first occurrence of a character from a string in TypeScript

1 Answer

0 votes
function removeFirstCharacter(str : string, char : string) {
    const found = str.indexOf(char);
     
    if (found !== -1) {
        return str.slice(0, found) + str.slice(found + 1);
    }
     
    return str;
}
 
 
let str = 'c++ c typescript python typescript php';
 
const char = 'j';
 
str = removeFirstCharacter(str, char);
 
console.log(str);
   
   
   
   
   
/*
run:
   
"c++ c typescript python typescript php" 
   
*/

 



answered Apr 16, 2022 by avibootz
edited Apr 16, 2022 by avibootz
...