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

167
Visualizações
Ellipsize middle of string in Javascriptt

I am currently trying to create a function that will ellipsize the middle of a text string. The idea is for it to show the first five characters and the last five characters. The middle of the string will just show as

...

So for example if I have dkjkjfdljfldkjfssdfsf

It should show as

dkjkj...sdfsf

I so far have this function:

function ellipsizeIt(text, length) { if (text.length > length) { return ${text.substring(0, length)}...${text.substring(0, length)}; } return text; }

But its just repeating the string first five characters when I do:

ellipsizeIt(text, 5);

It will show as

dkjkj...dkjkj

Any way to do it so that it doesnt just repeat the first five characters in the end of the string and show the actual last 5 characters?

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

0

From the code above there is a typo/logic issue where you had ${text.substring(0, length)}...${text.substring(0,length)};.

You are repeating yourself, so is the code.

update your code to

function ellipsizeIt(text, length) {
  if (text.length > length) {
    return `${text.substring(0, length)}...${text.substring(text.length -length)}`; 
  }
}

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