F# string building

I want to do this C# code in F#

  string[] a = new string[5];
    string b = string.Empty;
    a[0] = "Line 1";
    a[2] = "Line 2";

    foreach (string c in a)
    {
        b = c + Environment.NewLine;
    }

Answers


Its a lot better to use the built-in String.Join method than rolling your own function based on repeated string concatting. Here's the code in F#:

open System
let a = [| "Line 1"; null; "Line 2"; null; null;|] 
let b = String.Join(Environment.NewLine, a)

The '^' operator concatenates two strings. Also, '+' is overloaded so it can work on strings. But using a StringBuilder or Join is a better strategy for this.


You can use F#'s concat function from System module, like this:

let a = [| "Line 1"; null; "Line 2"; null; null;|] 
let b = String.concat System.Environment.NewLine a

(you should not import System namespace to avoid name conflict between F#'s String module and .NET's String class)


Need Your Help

Automatic click a button when the page loads with GreaseMonkey

jquery greasemonkey

I am new to Grease Monkey and I need to click this Delete button

Capture all $http premises with Angular

http angularjs callback

I would like to refresh the state of the user that is used in the navbar. but now I have to call .all(function() { refresh() }); on all $http.post premises.