Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6013309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:31:48+00:00 2026-05-23T02:31:48+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:31:48+00:00Added an answer on May 23, 2026 at 2:31 am

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that trim removes whitespace from the beginning and end of a string,
Is there a Python function that will trim whitespace (spaces and tabs) from a
How can I automatically .Trim() whitespace from the results of my Linq2SQL query? It
Possible Duplicate: Removing carriage return and new-line from the end of a string in
\n \n Expected:\n \n \n Q4\n \n \n 2011\n \n From that string, I
I want to trim trailing whitespace at the end of all XHTML paragraphs. I
I have a string that may have whitespace characters around it and I want
is there another way how to remove WhiteSpace Char(s) from String 1) other as
Microsoft Doc says about String.Trim Removes all leading and trailing white-space characters from the
When you use the Trim() method on a string object, you can pass an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.