Home
F#
String Examples: String.map
Updated Nov 21, 2023
Dot Net Perls
Strings. In the F# language we use .NET string methods like IndexOf and ToLower. We call these methods within functional constructs.
String notes. With String.map we can apply a mapping to all characters in a string. A lambda expression is used. We can chain string manipulations together in a pipeline.
First example. This example adds three string functions. In uppercase() it calls ToUpper. In addGreeting and addComma it adds some surrounding text to a string.
Info In the "let result" statement we pipeline the three methods together with a name argument (with value "visitor").
Result The 3 functions handle the string in order and the resulting text has been processed as expected.
printfn
// Three string manipulation functions. let uppercase (x : string) = x.ToUpper() let addGreeting (x : string) = "Hi " + x let addComma (x : string) = x + "," // Used to compute result. let name = "visitor" // Apply three string functions with pipeline operator on the name. let result = name |> uppercase |> addGreeting |> addComma // Write result. printfn "%A" result
"Hi VISITOR,"
String.map. In F# we find a String.map function that changes each character in a string according to a function argument. We use upperMap here to uppercase only the letters "a" and "z."
fun
Start We use pattern-matching in the upperMap function to determine how to modify the letter.
match
Tip With String.map we have an effective way to translate strings. For something like a ROT13 transformation this is ideal.
let value = "abcxyz" // This function only uppercases 2 letters. let upperMap = fun c -> match c with | 'a' -> 'A' | 'x' -> 'X' | _ -> c // Apply our upperMap function with String.map. let result = String.map upperMap value printfn "%A" result
"AbcXyz"
Methods. With F# we can invoke string methods from the .NET Framework. Some of the top methods are Split, IndexOf, Replace. These help us make programs that actually do things.
String IndexOf
String Split
String Replace
String Substring
Parse, convert int. With Int32.TryParse, we can convert a string to an int. No custom parsing logic is needed. The F# language requires special syntax here.
Convert
Ciphers, ROT13. With the ROT13 cipher, we rotate (shift) characters 13 places. We learn character-based manipulation of strings. With F# we employ the String.map function.
ROT13
Summary. We use strings in almost all programs, even functional F# ones. With the heavily-tested, fast string methods from .NET, we have a powerful string solution.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Nov 21, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen