String Substring ExampleCall Substring with 1 or 2 arguments to get a part of a string. Use it in a function with a type annotation.
This page was last reviewed on Nov 24, 2023.
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.
Slice syntax. 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.
Part 1 We use a single index (1) to access the second char of the word, which is the letter "b."
Part 2 The Chars() accessor can also be used. It works the same way as directly accessing an index.
Part 3 We can take a substring with slice syntax. The two arguments are the start index and the end index.
Part 4 For a substring of 2 chars, we specify the start index of 0 and then the value 2.
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
Method. 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.
Step 1 This is the string we want to take substrings from. The characters are "abcd" which makes the example easier to understand.
Step 2 We call Substring with 2 arguments—the start index, and the count past that start index. We use string interpolation to print the result.
Step 3 We call a let function that then calls Substring. A type annotation is required on getSubstringPastFirstTwo.
Step 4 We call a function that calls 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"
Error. 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.
Summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 24, 2023 (new example).
© 2007-2024 Sam Allen.