Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove white space from string

I would like to remove the whitespace located after \n.

For instance, username 123\n ugas 423\n peter 23\n asd234 would become username 123\nugas 423\npeter 23\nasd234.

like image 261
nicholas Avatar asked Jun 23 '10 04:06

nicholas


2 Answers

I am assuming you want to remove one or more whitespace characters at the beginning of each line, not just the first whitespace character. Also, I think you want to remove any kind of whitespace characters, like tabs, not just literal space characters.

import Data.Char

stripLeadingWhitespace :: String -> String
stripLeadingWhitespace = unlines . map (dropWhile isSpace) . lines
like image 184
Yitz Avatar answered Oct 24 '22 01:10

Yitz


f [] = []
f ('\n':' ':a) = f ('\n' : a)
f (a:b) = a : f b
like image 5
yfeldblum Avatar answered Oct 24 '22 01:10

yfeldblum