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

228
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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