Google News
logo
F# - Interview Questions
What are the String Indexing and Slicing in F#?
You can access individual characters in a string by using array-like syntax. The following examples use [] to index strings. This syntax was introduced in F# 6.0. You can also use .[] to index strings in all versions. The new syntax is preferred.
printfn "%c" str1[1]

The output is : b.
Or you can extract substrings by using array slice syntax, as shown in the following code.
printfn "%s" str1[0..2]
printfn "%s" str2[3..5]
The output is as follows :
abc
def
 
You can represent ASCII strings by arrays of unsigned bytes, type byte[]. You add the suffix B to a string literal to indicate that it's an ASCII string. ASCII string literals used with byte arrays support the same escape sequences as Unicode strings, except for the Unicode escape sequences.
// "abc" interpreted as a Unicode string.
let str1 : string = "abc"
// "abc" interpreted as an ASCII byte array.
let bytearray : byte[] = "abc"B
Advertisement