Home > Mobile >  how to remove a few characters from a sentece
how to remove a few characters from a sentece

Time:07-14

this is what i mean:

{input_value: hello,world}

func delete (target, wordstodelete): // delete wordstodelete from target

{new_input_value = delete ",world" from input_value}

so this is my file code:

<input id=“extension” type="file”>
<script>
Let e = document.getelementbyid(“extension”).value;
if(e.includes(“.atkp”)) {

}else{
alert(“this file can’t be accessed since it doesn’t have the extention ‘.atpk’.”)
}
</script>

i asked google and many other websites like w3schools. i found nothing useful except the substr() but it uses numbers. i want a function something like this: let e-edited = e.substr(".atkr"), instead does this let e-edited = e.substr(1, 3). i have nothing to start with. if i have to use the substr() method, then i have to make sure that the length is 10 or 8 characters and a name like: gghh.atkp or exampl.atkp.

CodePudding user response:

There are so many spelling errors in your code! Also, you should not check the value of the form before anyone has submitted the file. You can use an eventListener for that.

HTML

<label for="myfile">Select a file:</label>
<input type="file" id="extension" name="myfile"><br><br>

JS

let el = document.getElementById("extension");

el.addEventListener("change", (e) => {
    console.log(e.target.value)
  if(e.target.value.includes(".atpk")){
    console.log("this is an atpk")
  } else {
    console.log("this is not an atpk")
  }
})

CodePudding user response:

It depends on what you try to achieve,

If you want to delete all the occurrences of wordstodelete you can use string.replaceAll

If you want replace a single occurrence of the wordstodelete, then ou can use string.slice together with string.length or string.indexOf

Look at the examples below:

const input = 'filename.extension.filename.extension';
const toDelete = '.extension';


function deleteAllWords(target, wordstodelete) {
  return target.replaceAll(wordstodelete, '');
}
function deletFomTheEnd(target, wordstodelete) {
  return target.slice(0, -wordstodelete.length);
}
function deleteFromWord(target, wordstodelete) {
  const wordsIndex = target.indexOf(wordstodelete);
  return target.slice(0, wordsIndex);
}

console.log('deletFomTheEnd: ', deletFomTheEnd(input, toDelete));
console.log('deleteAllWords: ', deleteAllWords(input, toDelete));
console.log('deleteFromWord: ', deleteFromWord(input, toDelete));

  • Related