Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda