Substring
The Substring
method can be used in F# to get part of a string
based on a start index and a length. We can use a type annotation to help programs compile.
When calling Substring
in F# functions, we may encounter an error involving type annotations. This can be corrected by ensuring that the argument is known to be a string
.
We can access single chars in F# by using a index, and optionally using the Chars accessor. And we can access substrings with slice syntax.
char
of the word, which is the letter "b."Chars()
accessor can also be used. It works the same way as directly accessing an index.let word = "abcd" // Part 1: use single index to access char. let singleChar = word[1] printfn $"1 = {singleChar}" // Part 2: use Chars. let singleChar2 = word.Chars(1) printfn $"Chars(1) = {singleChar2}" // Part 3: use slice syntax with length 1. let singleCharSubstring = word[0..1] printfn $"0..1 = {singleCharSubstring}" // Part 4: use slice syntax with length 2. let middlePart = word[0..2] printfn $"0..2 = {middlePart}"1 = b Chars(1) = b 0..1 = ab 0..2 = abc
This example uses the Substring
method with one or two arguments. The first argument to Substring
is the start index of the part we want to receive.
string
we want to take substrings from. The characters are "abcd" which makes the example easier to understand.Substring
with 2 arguments—the start index, and the count past that start index. We use string
interpolation to print the result.Substring
. A type annotation is required on getSubstringPastFirstTwo
.Substring
, as well as the ToUpper
method, in a pipeline chain.// Specify a function that calls substring. let getSubstringPastFirstTwo (value : string) = value.Substring(2) // Define a pipelined function that includes substring. let uppercase (value : string) = value.ToUpper() let getUppercaseSubstring (value : string) = value |> getSubstringPastFirstTwo |> uppercase // Step 1: create a source string. let source = "abcd" // Step 2: call substring on a string with 2 arguments. let result1 = source.Substring(1, 2) printfn $"Substring: {result1}" // Step 3: call substring with 1 argument in a let function. let result2 = getSubstringPastFirstTwo source printfn $"Substring: {result2}" // Step 4: call substring in a pipelined function chain. let result3 = getUppercaseSubstring source printfn $"Substring, ToUpper: {result3}"Substring: "bc" Substring: "cd" Substring, ToUpper: "CD"
When using Substring
(or other string
methods) in let functions, we need to specify type annotations. This is because the compiler cannot determine which type Substring
refers to.
// This will not compile. // Use (value : string) instead. let getSubstring value = value.Substring(2)...Program.fs(3,26): error FS0072: Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved.
Much like in C#, we often use Substring
to get parts of strings. One or two arguments are used. And in F#, we may need type annotations to enable Substring
calls to compile correctly.