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

229
Vistas
change words at particular location in javascript string

var str = "Hello world";

str.charAt(2)="P"//instead of l i am assigning the value P

console.log(str)
I want to replace some Text from the string by checking some coditions but i am getting error i also tried replace function but that return nothing does no changes in the string

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need to make an array out of the string to replace by index. The following should do the trick.

const changeChar = (str, newValue, index) => {
  let splitted = str.split("");
  splitted[index] = newValue;
  return splitted.join("");
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

In JavaScript, strings are immutable, there are 2 ways you can do this which I have mentioned below

1 way can be to split the string using two substrings and stuff the character between them

var s = "Hello world";
var index = 2;
s = s.substring(0, index) + 'p' + s.substring(index + 1);
console.log(s)

2 way can be to convert the string to character array, replace one array member and join it

var str="Hello World"
str = str.split('');
str[2] = 'p';
str = str.join('');
console.log(str)
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