-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_examples.fs
46 lines (34 loc) · 1.46 KB
/
string_examples.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module string_examples
open System
let do_string_examples() =
// Escape characters
// \n, \\, \", \'
let str1 = "This is a random string"
printfn "str1 = %s" str1
// Verbatim Strings
let str2 = @"I ignore backslashes \t ok?"
printfn "str2 = %s" str2
// Triple Quoted Strings
let str3 = """ "I ignore double quotes and backslashes" """
printfn "str3 = %s" str3
// Combine strings
let str4 = str1 + " " + str2
printfn "str4 = %s" str4
// Get length
printfn "Length of str4 = %i" (String.length str4)
// Access index
printfn"1st (zero-indexed) character in str1 = %c" str1.[1]
// Get a substring with a range
printfn"Substring 0..3 in str1 = %s" (str1.[0..3])
// Collect executes a function on each character
let str1_char = String.collect (fun c -> sprintf "%c, " c) str1
printfn "Applying to each char in str1 : %s" str1_char
// Exists checks if any characters meet a condition
printfn "Does str1 contain uppercase characters? %b" (String.exists (fun c -> Char.IsUpper(c)) str1)
// Check if every character meets condition
printfn "Does str1 contain only numbers? %b" (String.forall (fun c -> Char.IsDigit(c)) str1)
// Apply function to each index in a string
let number_str = String.init 10 (fun i -> i.ToString())
printfn"Numbers : %s" number_str
// Apply function to each item in string
String.iter(fun c -> printfn "[%c]" c) number_str