Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
Understanding how to count the characters in a string in order to use slice() on a part of the string

I am learning JavaScript , the fundamentals for now and what I don’t get, is how to count the characters in the string in order to use the slice on the certain string and extract a certain word out of the string. For example

let text = "JavaScript", "apples", "avocado");
let newText = text.slice(?),(?);

How do I know or count the position of apples for example or JavaScript? Thank you!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try like this:

let text = "JavaScript, apples, avocado";
let newText = text.slice(text.indexOf("JavaScript"));
let newText2 = text.slice(text.indexOf("apples"));
console.log(newText)
console.log(newText2)

about 4 years ago · Juan Pablo Isaza Relatório

0

split the string into an array, , and then splice in the string you do want. Finally, join the array elements into a new string.

const str = 'JavaScript, apples, avocado';

function replace(str, search, replacement) {

  // Create an array using `split`
  const arr = str.split(', ');
  
  // Find the index of the item you're looking for
  const index = arr.findIndex(el => el === search);

  // `splice` in the new replacement string
  arr.splice(index, 1, replacement);

  // Return the new string
  return arr.join(', ');
  
}

console.log(replace(str, 'apples', 'biscuit'));

about 4 years ago · Juan Pablo Isaza Relatório

0

Few observations/suggestions :

  • String will always wrapped with the quotes but in your post it is not looks like a string.
  • Why you want to slice to get the word from that string ? You can convert that string into an array and then get that word.

Implementation steps :

  • Split string and convert that in an array using String.split() method.
  • Now we can find for the word you want to search from an array using Array.find() method.

Working Demo :

// Input string
const text = "JavaScript, apples, avocado";
const searchText = 'apples';

// Split string and convert that in an array using String.split() method.
const splittedStringArray = text.split(',');

// Now we can find for the word from splittedStringArray using Array.find() method.
const result = splittedStringArray.find(item => item.trim() === searchText);

// Output
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda