Reputation: 1320
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;
}
Upvotes: 4
Views: 2754
Reputation: 4977
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
)
Upvotes: 2
Reputation: 81516
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)
Upvotes: 14
Reputation: 118865
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.
Upvotes: 2