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

236
Visualizações
Write a program to trim both leading, trailing and extra whitespace characters from given string in JavaScript without using inbuilt methods?
  1. First I tried to understand the question as a beginner and tried to build logic to find the solution of the question.
  2. After that failing many times to build a proper logic .
  3. Then I started to find logic on google.
  4. And I get the logic for this question in C language here is the link https://codeforwin.org/2016/04/c-program-to-trim-both-leading-and-trailing-white-spaces-in-string.html
  5. But after trying many times I failed to convert C language code to JavaScript code as a beginner I understand that there is a very huge difference between C language and JavaScript language but I tried to implement the logic and finally, I failed.
  6. I want my code to print the string after trimming both leading, trailing, and extra whitespace characters.

here is my code-

var str = "     Lots of leading space!   ";
var index = 0;
var len = str.length-1;
var i = 0;
var j = 0;

while(str[index] == ' ' || str[index] == '\t' || str[index] == '\n'){
index++;
}

while(str[i + index] != len){
str[i] = str[i + index];
i++;
}

str[i] = len;
i = 0;

index = -1;
while(str[i] != len){
if(str[i] != ' ' && str[i] != '\t' && str[i] != '\n'){
        index = i;
    }

    i++;
}
str[index + 1] = len;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In javascript, strings are immutable. You cannot "replace" a character by writing something like str[0] = "A".

Since the C snippet you posted mostly relies on changing the string you give it, I think it'll be hard to convert directly.

Maybe it makes more sense to find the indexes of the leading and trailing white space, and then append each of the characters between those points to a new string?

var str = "     Lots of          leading space!   ";
var newStr = "";

var start = 0;
var end = str.length - 1;
var i;

// Find start
while(str[start] == ' ' || str[start] == '\t' || str[start] == '\n'){
  start++;
}

// Find end
while(str[end] == ' ' || str[end] == '\t' || str[end] == '\n'){
  end--;
}

// Build new string
i = start;
while (i <= end) {
  newStr += str[i];
  i++;
}

console.log(JSON.stringify(newStr));

Edit: To remove extra whitespace, you can modify the way you build your new string.

var str = "     Lots of          leading space!   ";
var newStr = "";

var start = 0;
var end = str.length - 1;
var i;

function isWhiteSpace(char) {
  return char == ' ' || char == '\t' || char == '\n';
}

// Find start
while(isWhiteSpace(str[start])) {
  start++;
}

// Find end
while(isWhiteSpace(str[end])) {
  end--;
}

// Build new string
i = start;
while (i <= end) {
  var char = str[i++];
  
  if (
    isWhiteSpace(char) &&
    isWhiteSpace(newStr[newStr.length - 1])
  ) continue;
  
  newStr += char;
}

console.log(JSON.stringify(newStr));

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