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

238
Vistas
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 Respuestas
Responde la pregunta

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