Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# string building

Tags:

string

f#

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;
    }
like image 255
TonyAbell Avatar asked Dec 02 '22 08:12

TonyAbell


2 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)
like image 133
Juliet Avatar answered Dec 28 '22 19:12

Juliet


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.

like image 42
Brian Avatar answered Dec 28 '22 17:12

Brian