Header menu logo 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 ")
val input: string
module String from Microsoft.FSharp.Core
val collect: mapping: (char -> string) -> str: string -> string
val sprintf: format: Printf.StringFormat<'T> -> 'T
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 ")
module String from Microsoft.FSharp.Core
val collect: mapping: (char -> string) -> str: string -> string
val chr: char
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
val sprintf: format: Printf.StringFormat<'T> -> 'T
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 : string seq - 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 : string seq

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"
val input1: string list
module String from Microsoft.FSharp.Core
val concat: sep: string -> strings: string seq -> string
val input2: string list
Multiple items
module List from Microsoft.FSharp.Collections

--------------------
type List<'T> = | op_Nil | op_ColonColon of Head: 'T * Tail: 'T list interface IReadOnlyList<'T> interface IReadOnlyCollection<'T> interface IEnumerable interface IEnumerable<'T> member GetReverseIndex: rank: int * offset: int -> int member GetSlice: startIndex: int option * endIndex: int option -> 'T list static member Cons: head: 'T * tail: 'T list -> 'T list member Head: 'T member IsEmpty: bool member Item: index: int -> 'T with get ...
val map: mapping: ('T -> 'U) -> list: 'T list -> 'U list
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
val input3: string list

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
namespace System
Multiple items
type String = interface IEnumerable<char> interface IEnumerable interface ICloneable interface IComparable interface IComparable<string> interface IConvertible interface IEquatable<string> interface IParsable<string> interface ISpanParsable<string> new: value: nativeptr<char> -> unit + 8 overloads ...
<summary>Represents text as a sequence of UTF-16 code units.</summary>

--------------------
String(value: nativeptr<char>) : String
String(value: char array) : String
String(value: ReadOnlySpan<char>) : String
String(value: nativeptr<sbyte>) : String
String(c: char, count: int) : String
String(value: nativeptr<char>, startIndex: int, length: int) : String
String(value: char array, startIndex: int, length: int) : String
String(value: nativeptr<sbyte>, startIndex: int, length: int) : String
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : String
val exists: predicate: (char -> bool) -> str: string -> bool
[<Struct>] type Char = member CompareTo: value: char -> int + 1 overload member Equals: obj: char -> bool + 1 overload member GetHashCode: unit -> int member GetTypeCode: unit -> TypeCode member ToString: unit -> string + 2 overloads static member ConvertFromUtf32: utf32: int -> string static member ConvertToUtf32: highSurrogate: char * lowSurrogate: char -> int + 1 overload static member GetNumericValue: c: char -> float + 1 overload static member GetUnicodeCategory: c: char -> UnicodeCategory + 1 overload static member IsAscii: c: char -> bool ...
<summary>Represents a character as a UTF-16 code unit.</summary>
Char.IsUpper(c: char) : bool
Char.IsUpper(s: string, index: int) : bool

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"
namespace System
val input: string
Multiple items
type String = interface IEnumerable<char> interface IEnumerable interface ICloneable interface IComparable interface IComparable<string> interface IConvertible interface IEquatable<string> interface IParsable<string> interface ISpanParsable<string> new: value: nativeptr<char> -> unit + 8 overloads ...
<summary>Represents text as a sequence of UTF-16 code units.</summary>

--------------------
String(value: nativeptr<char>) : String
String(value: char array) : String
String(value: ReadOnlySpan<char>) : String
String(value: nativeptr<sbyte>) : String
String(c: char, count: int) : String
String(value: nativeptr<char>, startIndex: int, length: int) : String
String(value: char array, startIndex: int, length: int) : String
String(value: nativeptr<sbyte>, startIndex: int, length: int) : String
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : String
val filter: predicate: (char -> bool) -> str: string -> string
Multiple items
type Uri = interface IFormattable interface ISpanFormattable interface IEquatable<Uri> interface ISerializable new: uriString: string -> unit + 6 overloads member Equals: comparand: obj -> bool + 1 overload member GetComponents: components: UriComponents * format: UriFormat -> string member GetHashCode: unit -> int member GetLeftPart: part: UriPartial -> string member IsBaseOf: uri: Uri -> bool ...
<summary>Provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.</summary>

--------------------
Uri(uriString: string) : Uri
Uri(uriString: string, creationOptions: inref<UriCreationOptions>) : Uri
Uri(uriString: string, uriKind: UriKind) : Uri
Uri(baseUri: Uri, relativeUri: string) : Uri
Uri(baseUri: Uri, relativeUri: Uri) : Uri
Uri.IsHexDigit(character: char) : bool

Example

Filtering out just digits

 open System

 "hello" |> String.filter Char.IsDigit  // evaluates ""
namespace System
Multiple items
type String = interface IEnumerable<char> interface IEnumerable interface ICloneable interface IComparable interface IComparable<string> interface IConvertible interface IEquatable<string> interface IParsable<string> interface ISpanParsable<string> new: value: nativeptr<char> -> unit + 8 overloads ...
<summary>Represents text as a sequence of UTF-16 code units.</summary>

--------------------
String(value: nativeptr<char>) : String
String(value: char array) : String
String(value: ReadOnlySpan<char>) : String
String(value: nativeptr<sbyte>) : String
String(c: char, count: int) : String
String(value: nativeptr<char>, startIndex: int, length: int) : String
String(value: char array, startIndex: int, length: int) : String
String(value: nativeptr<sbyte>, startIndex: int, length: int) : String
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : String
val filter: predicate: (char -> bool) -> str: string -> string
[<Struct>] type Char = member CompareTo: value: char -> int + 1 overload member Equals: obj: char -> bool + 1 overload member GetHashCode: unit -> int member GetTypeCode: unit -> TypeCode member ToString: unit -> string + 2 overloads static member ConvertFromUtf32: utf32: int -> string static member ConvertToUtf32: highSurrogate: char * lowSurrogate: char -> int + 1 overload static member GetNumericValue: c: char -> float + 1 overload static member GetUnicodeCategory: c: char -> UnicodeCategory + 1 overload static member IsAscii: c: char -> bool ...
<summary>Represents a character as a UTF-16 code unit.</summary>
Char.IsDigit(c: char) : bool
Char.IsDigit(s: string, index: int) : bool

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
namespace System
Multiple items
type String = interface IEnumerable<char> interface IEnumerable interface ICloneable interface IComparable interface IComparable<string> interface IConvertible interface IEquatable<string> interface IParsable<string> interface ISpanParsable<string> new: value: nativeptr<char> -> unit + 8 overloads ...
<summary>Represents text as a sequence of UTF-16 code units.</summary>

--------------------
String(value: nativeptr<char>) : String
String(value: char array) : String
String(value: ReadOnlySpan<char>) : String
String(value: nativeptr<sbyte>) : String
String(c: char, count: int) : String
String(value: nativeptr<char>, startIndex: int, length: int) : String
String(value: char array, startIndex: int, length: int) : String
String(value: nativeptr<sbyte>, startIndex: int, length: int) : String
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : String
val forall: predicate: (char -> bool) -> str: string -> bool
[<Struct>] type Char = member CompareTo: value: char -> int + 1 overload member Equals: obj: char -> bool + 1 overload member GetHashCode: unit -> int member GetTypeCode: unit -> TypeCode member ToString: unit -> string + 2 overloads static member ConvertFromUtf32: utf32: int -> string static member ConvertToUtf32: highSurrogate: char * lowSurrogate: char -> int + 1 overload static member GetNumericValue: c: char -> float + 1 overload static member GetUnicodeCategory: c: char -> UnicodeCategory + 1 overload static member IsAscii: c: char -> bool ...
<summary>Represents a character as a UTF-16 code unit.</summary>
Char.IsLower(c: char) : bool
Char.IsLower(s: string, index: int) : bool

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 ")
module String from Microsoft.FSharp.Core
val init: count: int -> initializer: (int -> string) -> string
val i: int
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
val sprintf: format: Printf.StringFormat<'T> -> 'T
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 character in the string

 let input = "Hello"
 input |> String.iter (fun c -> printfn "%c %d" c (int c))
val input: string
module String from Microsoft.FSharp.Core
val iter: action: (char -> unit) -> str: string -> unit
val c: char
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
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 character in the input string

 let input = "Hello"
 input |> String.iteri (fun i c -> printfn "%d. %c %d" (i + 1) c (int c))
val input: string
module String from Microsoft.FSharp.Core
val iteri: action: (int -> char -> unit) -> str: string -> unit
val i: int
val c: char
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
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
module String from Microsoft.FSharp.Core
val length: str: string -> int

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!"
namespace System
val input: string
Multiple items
type String = interface IEnumerable<char> interface IEnumerable interface ICloneable interface IComparable interface IComparable<string> interface IConvertible interface IEquatable<string> interface IParsable<string> interface ISpanParsable<string> new: value: nativeptr<char> -> unit + 8 overloads ...
<summary>Represents text as a sequence of UTF-16 code units.</summary>

--------------------
String(value: nativeptr<char>) : String
String(value: char array) : String
String(value: ReadOnlySpan<char>) : String
String(value: nativeptr<sbyte>) : String
String(c: char, count: int) : String
String(value: nativeptr<char>, startIndex: int, length: int) : String
String(value: char array, startIndex: int, length: int) : String
String(value: nativeptr<sbyte>, startIndex: int, length: int) : String
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : String
val map: mapping: (char -> char) -> str: string -> string
[<Struct>] type Char = member CompareTo: value: char -> int + 1 overload member Equals: obj: char -> bool + 1 overload member GetHashCode: unit -> int member GetTypeCode: unit -> TypeCode member ToString: unit -> string + 2 overloads static member ConvertFromUtf32: utf32: int -> string static member ConvertToUtf32: highSurrogate: char * lowSurrogate: char -> int + 1 overload static member GetNumericValue: c: char -> float + 1 overload static member GetUnicodeCategory: c: char -> UnicodeCategory + 1 overload static member IsAscii: c: char -> bool ...
<summary>Represents a character as a UTF-16 code unit.</summary>
Char.ToUpper(c: char) : char
Char.ToUpper(c: char, culture: Globalization.CultureInfo) : char

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))
module String from Microsoft.FSharp.Core
val mapi: mapping: (int -> char -> char) -> str: string -> string
val i: int
val c: char
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
module String from Microsoft.FSharp.Core
val replicate: count: int -> str: string -> string
Evaluates to "Do it!Do it!Do it!".

Type something to start searching.