Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!