Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Haskell, how do you trim whitespace from the beginning and end of a string?

How do you trim whitespace from the start and end of a string?

trim "  abc "   =>  "abc" 

Edit:

Ok, let me be a little clearer. I did not understand that string literals were treated so differently from Strings.

I would like to do this:

import qualified Data.Text as T let s :: String = "  abc  " in T.strip s 

Is this possible in Haskell? I am using -XOverloadedStrings but that appears only to work for literals.

like image 984
Eric Normand Avatar asked Jun 07 '11 19:06

Eric Normand


People also ask

How do you remove whitespace from the beginning and end of a string?

String result = str. trim(); The trim() method will remove both leading and trailing whitespace from a string and return the result.

How do you remove spaces from a string Haskell?

Haskell Language Data.strip removes whitespace from the start and end of a Text value. stripStart removes whitespace only from the start. stripEnd removes whitespace only from the end. filter can be used to remove whitespace, or other characters, from the middle.

Which method is used to remove whitespace from both ends of a string?

trim() method removes the leading and trailing spaces present in the string.

How do you trim a space in a string?

The trim() method in Java String is a built-in function that eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in java checks this Unicode value before and after the string, if it exists then removes the spaces and returns the omitted string.


2 Answers

If you have serious text processing needs then use the text package from hackage:

> :set -XOverloadedStrings > import Data.Text > strip "  abc   " "abc" 

If you're too stubborn to use text and don't like the inefficiency of the reverse method then perhaps (and I mean MAYBE) something like the below will be more efficient:

import Data.Char  trim xs = dropSpaceTail "" $ dropWhile isSpace xs  dropSpaceTail maybeStuff "" = "" dropSpaceTail maybeStuff (x:xs)         | isSpace x = dropSpaceTail (x:maybeStuff) xs         | null maybeStuff = x : dropSpaceTail "" xs         | otherwise       = reverse maybeStuff ++ x : dropSpaceTail "" xs   > trim "  hello this \t should trim ok.. .I  think  ..  \t " "hello this \t should trim ok.. .I  think  .." 

I wrote this on the assumption that the length of spaces would be minimal, so your O(n) of ++ and reverse is of little concern. But once again I feel the need to say that if you actually are concerned about the performance then you shouldn't be using String at all - move to Text.

EDIT making my point, a quick Criterion benchmark tells me that (for a particularly long string of words with spaces and ~200 pre and post spaces) my trim takes 1.6 ms, the trim using reverse takes 3.5ms, and Data.Text.strip takes 0.0016 ms...

like image 56
Thomas M. DuBuisson Avatar answered Oct 06 '22 06:10

Thomas M. DuBuisson


From: http://en.wikipedia.org/wiki/Trim_(programming)#Haskell

import Data.Char (isSpace)  trim :: String -> String trim = f . f    where f = reverse . dropWhile isSpace 
like image 36
Eric Normand Avatar answered Oct 06 '22 06:10

Eric Normand