Export (0) Print
Expand All

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

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.

Exception

Condition

ArgumentException

Thrown when count is negative.

The constructed string.

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.

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

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

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

Show:
© 2015 Microsoft