Table of contents
Collapse the table of content
Expand the table of content

String.forall Function (F#)

Den Delimarsky|Last Updated: 8/18/2017
1 Contributor

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

Namespace/Module Path: Microsoft.FSharp.Core.String

Assembly: FSharp.Core (in FSharp.Core.dll)


// Signature:
String.forall : (char -> bool) -> string -> bool

// Usage:
String.forall predicate str


predicate Type: char->bool

The function to test each character of the string.

str Type: string

The input string.


ArgumentNullExceptionThrown when the input string is null.

Return Value

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


This function is named ForAll in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.


The following code shows how to use String.forall.

let isWholeNumber string1 =
    if (String.forall (fun c -> System.Char.IsDigit(c)) string1) then
        printfn "The string \"%s\" is a whole number." string1
        printfn "The string \"%s\" is not a valid whole number." string1
isWholeNumber "8005"
isWholeNumber "512"
isWholeNumber "0x20"
isWholeNumber "1.0E-5"
isWholeNumber "-20"


The string "8005" is a whole number.
The string "512" is a whole number.
The string "0x20" is not a valid whole number.
The string "1.0E-5" is not a valid whole number.
The string "-20" is not a valid whole number.


Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Core.String Module (F#)

Microsoft.FSharp.Core Namespace (F#)

© 2020 Microsoft