String.init Function (F#)

Creates a new string whose characters are the results of applying a specified function to each index and concatenating the resulting strings.

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

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

// Signature:
String.init : int -> (int -> string) -> string

// Usage:
String.init count initializer

Parameters

  • count
    Type: int

    The number of strings to initialize.

  • initializer
    Type: int -> string

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

Exceptions

Exception

Condition

ArgumentException

Thrown when count is negative.

Return Value

The constructed string.

Remarks

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

Example

The following code shows how to use String.init.

let string1 = String.init 10 (fun i -> i.ToString())
printfn "%s" string1
let string2 = String.init 26 (fun i ->
    sprintf "%c" (char (i + int 'A')))
printfn "%s" string2

Output

0123456789
ABCDEFGHIJKLMNOPQRSTUVWXYZ

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#)