FSharp.Core


String Module

Functional programming operators for string processing. Further string operations are available via the member functions on strings and other functionality in System.String and System.Text.RegularExpressions types.

Functions and values

Function or value Description

String.collect mapping str

Full Usage: String.collect mapping str

Parameters:
    mapping : char -> string - The function to produce a string from each character of the input string.
    str : string - The input string.

Returns: string The concatenated string.

Builds a new string whose characters are the results of applying the function mapping to each of the characters of the input string and concatenating the resulting strings.

mapping : char -> string

The function to produce a string from each character of the input string.

str : string

The input string.

Returns: string

The concatenated string.

Example

The following samples shows how to interspace spaces in a text

 let input = "Stefan says: Hi!"
 
 input |> String.collect (sprintf "%c ")
The sample evaluates to "S t e f a n s a y s : H i ! "

Example

How to show the ASCII representation of a very secret text

 "Secret" |> String.collect (fun chr -> int chr |> sprintf "%d ")
The sample evaluates to "83 101 99 114 101 116 "

String.concat sep strings

Full Usage: String.concat sep strings

Parameters:
    sep : string - The separator string to be inserted between the strings of the input sequence.
    strings : seq<string> - The sequence of strings to be concatenated.

Returns: string A new string consisting of the concatenated strings separated by the separation string.

Returns a new string made by concatenating the given strings with separator sep, that is a1 + sep + ... + sep + aN.

sep : string

The separator string to be inserted between the strings of the input sequence.

strings : seq<string>

The sequence of strings to be concatenated.

Returns: string

A new string consisting of the concatenated strings separated by the separation string.

ArgumentNullException Thrown when strings is null.
Example

 let input1 = ["Stefan"; "says:"; "Hello"; "there!"]
 
 input1 |> String.concat " "  // evaluates "Stefan says: Hello there!"
 
 let input2 = [0..9] |> List.map string 
 
 input2 |> String.concat ""    // evaluates "0123456789"
 input2 |> String.concat ", "  // evaluates "0, 1, 2, 3, 4, 5, 6, 7, 8, 9"
 
 let input3 = ["No comma"]
 
 input3 |> String.concat ","   // evaluates "No comma"

String.exists predicate str

Full Usage: String.exists predicate str

Parameters:
    predicate : char -> bool - The function to test each character of the string.
    str : string - The input string.

Returns: bool True if any character returns true for the predicate and false otherwise.

Tests if any character of the string satisfies the given predicate.

predicate : char -> bool

The function to test each character of the string.

str : string

The input string.

Returns: bool

True if any character returns true for the predicate and false otherwise.

Example

Looking for uppercase characters

 open System
 
 "Yoda" |> String.exists Char.IsUpper  // evaluates true
 
 "nope" |> String.exists Char.IsUpper  // evaluates false

String.filter predicate str

Full Usage: String.filter predicate str

Parameters:
    predicate : char -> bool - A function to test whether each character in the input sequence should be included in the output string.
    str : string - The input string.

Returns: string The resulting string.

Builds a new string containing only the characters of the input string for which the given predicate returns "true".

Returns an empty string if the input string is null

predicate : char -> bool

A function to test whether each character in the input sequence should be included in the output string.

str : string

The input string.

Returns: string

The resulting string.

Example

Filtering out just alphanumeric characters

 open System
 
 let input = "0 1 2 3 4 5 6 7 8 9 a A m M"
 
 input |> String.filter Uri.IsHexDigit  // evaluates "123456789aA"

Example

Filtering out just digits

 open System
 
 "hello" |> String.filter Char.IsDigit  // evaluates ""

String.forall predicate str

Full Usage: String.forall predicate str

Parameters:
    predicate : char -> bool - The function to test each character of the string.
    str : string - The input string.

Returns: bool True if all characters return true for the predicate and false otherwise.

Tests if all characters in the string satisfy the given predicate.

predicate : char -> bool

The function to test each character of the string.

str : string

The input string.

Returns: bool

True if all characters return true for the predicate and false otherwise.

Example

Looking for lowercase characters

 open System
 
 "all are lower" |> String.forall Char.IsLower  // evaluates false
 
 "allarelower" |> String.forall Char.IsLower    // evaluates true

String.init count initializer

Full Usage: String.init count initializer

Parameters:
    count : int - The number of strings to initialize.
    initializer : int -> string - The function to take an index and produce a string to be concatenated with the others.

Returns: string The constructed string.

Builds a new string whose characters are the results of applying the function mapping to each index from 0 to count-1 and concatenating the resulting strings.

count : int

The number of strings to initialize.

initializer : int -> string

The function to take an index and produce a string to be concatenated with the others.

Returns: string

The constructed string.

ArgumentException Thrown when count is negative.
Example

Enumerate digits ASCII codes

 String.init 10 (fun i -> int '0' + i |> sprintf "%d ")
The sample evaluates to: "48 49 50 51 52 53 54 55 56 57 "

String.iter action str

Full Usage: String.iter action str

Parameters:
    action : char -> unit - The function to be applied to each character of the string.
    str : string - The input string.

Applies the function action to each character in the string.

action : char -> unit

The function to be applied to each character of the string.

str : string

The input string.

Example

Printing the ASCII code for each characater in the string

 let input = "Hello"
 input |> String.iter (fun c -> printfn "%c %d" c (int c))
The sample evaluates as unit, but prints:
 H 72
 e 101
 l 108
 l 108
 o 111

String.iteri action str

Full Usage: String.iteri action str

Parameters:
    action : int -> char -> unit - The function to apply to each character and index of the string.
    str : string - The input string.

Applies the function action to the index of each character in the string and the character itself.

action : int -> char -> unit

The function to apply to each character and index of the string.

str : string

The input string.

Example

Numbering the characters and printing the associated ASCII code for each characater in the input string

 let input = "Hello"
 input |> String.iteri (fun i c -> printfn "%d. %c %d" (i + 1) c (int c))
The sample evaluates as unit, but prints:
 1. H 72
 2. e 101
 3. l 108
 4. l 108
 5. o 111

String.length str

Full Usage: String.length str

Parameters:
    str : string - The input string.

Returns: int The number of characters in the string.

Returns the length of the string.

str : string

The input string.

Returns: int

The number of characters in the string.

Example

Getting the length of different strings

 String.length null   // evaluates 0
 String.length ""     // evaluates 0
 String.length "123"  // evaluates 3

String.map mapping str

Full Usage: String.map mapping str

Parameters:
    mapping : char -> char - The function to apply to the characters of the string.
    str : string - The input string.

Returns: string The resulting string.

Builds a new string whose characters are the results of applying the function mapping to each of the characters of the input string.

mapping : char -> char

The function to apply to the characters of the string.

str : string

The input string.

Returns: string

The resulting string.

Example

Changing case to upper for all characters in the input string

 open System
 
 let input = "Hello there!"
 
 input |> String.map Char.ToUpper  // evaluates "HELLO THERE!"

String.mapi mapping str

Full Usage: String.mapi mapping str

Parameters:
    mapping : int -> char -> char - The function to apply to each character and index of the string.
    str : string - The input string.

Returns: string The resulting string.

Builds a new string whose characters are the results of applying the function mapping to each character and index of the input string.

mapping : int -> char -> char

The function to apply to each character and index of the string.

str : string

The input string.

Returns: string

The resulting string.

Example

 input |> String.mapi (fun i c -> (i, c))
Evaluates to [ (0, 'O'); (1, 'K'); (2, '!') ].

String.replicate count str

Full Usage: String.replicate count str

Parameters:
    count : int - The number of copies of the input string will be copied.
    str : string - The input string.

Returns: string The concatenated string.

Returns a string by concatenating count instances of str.

count : int

The number of copies of the input string will be copied.

str : string

The input string.

Returns: string

The concatenated string.

ArgumentException Thrown when count is negative.
Example

 "Do it!" |> String.replicate 3
Evaluates to "Do it!Do it!Do it!".