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

140
Visualizações
How to parse a string in JavaScript based on comma as a delimiter?

Is there any way in JavaScript to parse a string?

For example, I have this string named "band":

let band = "John,Paul,George,Ringo";

and I want to parse it based on comma as a delimiter. So, in the end I want to have four variables:

let name1 = "John";
let name2 = "Paul";
let name3 = "George";
let name4 = "Ringo";

How can I do that?

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

0

You can split the string on a comma:

let band = "John,Paul,George,Ringo";
let members = band.split(',');
let john = members[0];
let paul = members[1];
let george = members[2];
let ringo = members[3];

This will give you an array called members which lets you access them using an index either directly or via a loop.

Depending on what browsers/environments you may need to support, you can destructure the array too:

let band = "John,Paul,George,Ringo";
let [john, paul, george, ringo] = band.split(',');

If you want loop over the members:

let band = "John,Paul,George,Ringo";
let members = band.split(',');

for (let i = 0; i < members.length; i++) {
    console.log(members[i]);
}

This would loop over all members in the array and log them to console, which you can modify to perform other things on each item in the array.

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