Udostępnij za pośrednictwem


String.map Function (F#)

Creates a new string whose characters are the results of applying a specified function to each of the characters of the input string.

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

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

// Signature:
String.map : (char -> char) -> string -> string

// Usage:
String.map mapping str

Parameters

  • mapping
    Type: char -> char

    The function to apply to the characters of the string.

  • str
    Type: string

    The input string.

Exceptions

Exception

Condition

ArgumentNullException

Thrown when the input string is null.

Return Value

The resulting string.

Remarks

This function is named Map 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.map.

let rot13 c =
    let upperZero = int 'A' - 1
    let lowerZero = int 'a' - 1
    if System.Char.IsLetter(c) then
        if System.Char.IsUpper(c) then
            char (((int c + 13 - upperZero) % 26) + upperZero)
        else
            char (((int c + 13 - lowerZero) % 26) + lowerZero)
    else c
let test = "The quick sly fox jumped over the lazy brown dog."
printfn "%s" test
printfn "%s" <| (String.map rot13 test)

Output

The quick sly fox jumped over the lazy brown dog.
Gur dhvpx fyl sbk whzcrq bire gur ynml oebja qbt.

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Core.String Module (F#)

Microsoft.FSharp.Core Namespace (F#)