Saturday, March 9, 2019

Remove control characters from a string

function removeCC(s) {
  /* Remove NewLine, Carriage Return and Tab characters from a String  */

  r = "";
  for (i=0; i < s.length; i++) {
    if (s.charAt(i) != '\n' &&
        s.charAt(i) != '\r' &&
        s.charAt(i) != '\t') {
      r += s.charAt(i);
      }
    }
  return r;
  }

Same thing with regular expression


function removeCC(s){ 
  return s.replace(/[\n\r\t]/g," "); 
}

No comments:

Post a Comment

Your feedback is always appreciated. I will try to reply to your queries as soon as time allows.Please don't spam,spam comments will be deleted upon reviews.