Skip to main content
ubuntuask.com

Back to all posts

How to Split A String In Haskell?

Published on
4 min read
How to Split A String In Haskell? image

Best String Manipulation Resources to Buy in September 2026

1 Programming in Haskell

Programming in Haskell

BUY & SAVE
$26.99 $47.00
Save 43%
Programming in Haskell
2 Get Programming with Haskell

Get Programming with Haskell

BUY & SAVE
$44.99
Get Programming with Haskell
3 Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell

Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell

BUY & SAVE
$34.80 $49.99
Save 30%
Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell
4 Real World Haskell

Real World Haskell

  • AFFORDABLE PRICES: QUALITY READING AT A FRACTION OF THE COST.
  • ECO-FRIENDLY CHOICE: SUPPORT SUSTAINABILITY WITH PRE-LOVED BOOKS.
  • DIVERSE SELECTION: DISCOVER HIDDEN GEMS ACROSS VARIOUS GENRES.
BUY & SAVE
$36.34 $49.99
Save 27%
Real World Haskell
5 Learn You a Haskell for Great Good!: A Beginner's Guide

Learn You a Haskell for Great Good!: A Beginner's Guide

  • QUALITY ASSURANCE: EACH BOOK IS THOROUGHLY INSPECTED FOR QUALITY.
  • AFFORDABLE PRICES: SAVE MONEY WHILE ENJOYING GREAT LITERARY FINDS.
  • ECO-FRIENDLY CHOICE: CHOOSE USED BOOKS TO REDUCE ENVIRONMENTAL IMPACT.
BUY & SAVE
$33.48 $49.99
Save 33%
Learn You a Haskell for Great Good!: A Beginner's Guide
6 Haskell in Depth

Haskell in Depth

BUY & SAVE
$59.83
Haskell in Depth
7 Functional Programming with Haskell: A Practical Guide to Writing Type-Safe, Modular, and Maintainable Code

Functional Programming with Haskell: A Practical Guide to Writing Type-Safe, Modular, and Maintainable Code

BUY & SAVE
$7.99 $21.99
Save 64%
Functional Programming with Haskell: A Practical Guide to Writing Type-Safe, Modular, and Maintainable Code
8 Production Haskell: Succeeding in Industry with Haskell

Production Haskell: Succeeding in Industry with Haskell

BUY & SAVE
$32.44 $49.00
Save 34%
Production Haskell: Succeeding in Industry with Haskell
+
ONE MORE?

In Haskell, you can split a string into parts using various methods. Here are three common approaches:

  1. Using the words function: The words function in Haskell splits a string into a list of words. Each word in the string is separated by one or more whitespace characters. For example: words :: String -> [String] words "Hello World" -- Returns: ["Hello","World"] words splits the string at each whitespace character and returns a list of words as separate strings.
  2. Using splitOn function from the Data.List.Split module: The splitOn function allows you to split a string based on a specific delimiter. You'll need to import the Data.List.Split module to use this function. For example: import Data.List.Split splitOn :: Eq a => [a] -> [a] -> [[a]] splitOn " " "Hello World" -- Returns: ["Hello","World"] Here, splitOn takes two arguments - the delimiter and the string. It splits the string wherever the delimiter is found and returns a list of the resulting parts.
  3. Using the split function from the Data.List module: The split function lets you split a string based on a predicate function. Again, you'll need to import the Data.List module to use this function. For example: import Data.List split :: (a -> Bool) -> [a] -> [[a]] split (== ' ') "Hello World" -- Returns: ["Hello","World"] In this example, split takes two arguments - a predicate function and the string. It splits the string wherever the predicate function evaluates to True, resulting in a list of separated parts.

These are some common methods to split a string in Haskell. Depending on your specific requirements, you can choose the approach that suits your needs.

How do you split a string into two parts based on a specific pattern in Haskell?

To split a string into two parts based on a specific pattern in Haskell, you can use the split function from the Data.List.Split module. Here's an example of how to split a string based on a specific pattern:

import Data.List.Split

splitString :: String -> String -> (String, String) splitString pattern input = case split (oneOf pattern) input of [part1, part2] -> (part1, part2) _ -> ("", "")

main :: IO () main = do let string = "Hello, World!" let (part1, part2) = splitString ", " string putStrLn part1 putStrLn part2

In this example, the splitString function takes two arguments: the pattern you want to split on and the input string. It uses the split function with oneOf pattern to split the input string into a list of parts. Then, it pattern matches on the list to extract the first two parts, assuming there are exactly two parts. If there are not exactly two parts, it returns two empty strings.

In the main function, an example string "Hello, World!" is split into two parts using the pattern ", ". The two parts are printed separately using putStrLn. The output of this program would be:

Hello World!

Note that you need to install the split package if you haven't already done so. You can install it using the following command:

$ cabal install split

How do you split a string and convert the resulting substrings into integers?

To split a string and convert the resulting substrings into integers, you can use the following steps:

  1. Use the split() function on the string to split it into a list of substrings. The split() function takes a delimiter as an argument and splits the string into substrings based on that delimiter. For example:

string = "10 20 30 40 50" substrings = string.split(" ")

  1. Iterate over each substring in the list using a loop or list comprehension.
  2. Use the int() function to convert each substring into an integer. The int() function takes a string as an argument and returns its integer representation. For example:

numbers = [] for substring in substrings: numbers.append(int(substring))

or

numbers = [int(substring) for substring in substrings]

Now, the numbers list will contain the converted integers. In the given example, the numbers list will be [10, 20, 30, 40, 50].

What happens if the delimiter is not found in the string being split?

If the delimiter is not found in the string being split, the original string is returned as a single element in the result array.