Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F#: Remove the first N characters from a string?

Tags:

string

f#

I'm trying to write some code to remove the first N characters in a string. I could have done this in an imperative manner already, but I would like to see it done in the spirit of functional programming. Being new to F# and functional programming, I'm having some trouble...

like image 371
rysama Avatar asked Nov 01 '09 03:11

rysama


3 Answers

"Hello world".[n..];;
like image 61
Jeff Avatar answered Nov 06 '22 02:11

Jeff


As @Jeff has shown, you can do this in six characters, so this is not necessarily the best question to ask to see how to "do it in the spirit of functional programming".

I show another way, which is not particularly "functional" (as it uses arrays, but at least it doesn't mutate any), but at least shows a set of steps.

let s = "Hello, world!"
// get array of chars
let a = s.ToCharArray()
// get sub array (start char 7, 5 long)
let a2 = Array.sub a 7 5
// make new string
let s2 = new string(a2)
printfn "-%s-" s2  // -world-
like image 45
Brian Avatar answered Nov 06 '22 04:11

Brian


"Hello world".Substring 3
like image 2
The_Ghost Avatar answered Nov 06 '22 03:11

The_Ghost