Compartilhar via


String.forall Function (F#)

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

Parameters

  • predicate
    Type: char -> bool

    The function to test each character of the string.

  • str
    Type: string

    The input string.

Exceptions

Exception

Condition

ArgumentNullException

Thrown when the input string is null.

Return Value

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

Remarks

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.

Example

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
    else
        printfn "The string \"%s\" is not a valid whole number." string1
isWholeNumber "8005"
isWholeNumber "512"
isWholeNumber "0x20"
isWholeNumber "1.0E-5"
isWholeNumber "-20"

Output

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.

Platforms

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

Reference

Core.String Module (F#)

Microsoft.FSharp.Core Namespace (F#)