My goal is to find the number of times a substring exists within a string. The substring I'm looking for will be of type "[n]", where n can be any variable.
My attempt involved splitting the string up using the words function, then create a new list of strings if the 'head' of a string was '[' and the 'last' of the same string was ']'
The problem I ran into was that I entered a String which when split using the function words, created a String that looked like this "[2]," Now, I still want this to count as an occurrence of the type "[n]"
An example would be I would want this String,
asdf[1]jkl[2]asdf[1]jkl
to return 3.
Here's the code I have:
-- String that will be tested on references function
txt :: String
txt = "[1] and [2] both feature characters who will do whatever it takes to " ++
"get to their goal, and in the end the thing they want the most ends " ++
"up destroying them. In case of [2], this is a whale..."
-- Function that will take a list of Strings and return a list that contains
-- any String of the type [n], where n is an variable
ref :: [String] -> [String]
ref [] = []
ref xs = [x | x <- xs, head x == '[', last x == ']']
-- Function takes a text with references in the format [n] and returns
-- the total number of references.
-- Example : ghci> references txt -- -> 3
references :: String -> Integer
references txt = len (ref (words txt))
If anyone can enlighten me on how to search for a substring within a string or how to parse a string given a substring, that would be greatly appreciated.