Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate strings in ocaml with newline between them

Tags:

ocaml

I'd like to do something like this

String.concat '\n' [str1; str2 ... strn]

so I can print in a file. But ocaml doesn't allow me to do that. What can I do?

like image 523
EBM Avatar asked Apr 03 '12 02:04

EBM


People also ask

How do I combine two strings in Ocaml?

The (^) binary operator concatenates two strings.

How do I concatenate a new line in a string?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

Does concatenation create a new string?

In Java, String concatenation forms a new String that is the combination of multiple strings. There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.

How do I add a space between concatenated strings?

There are two ways to do this: Add double quotation marks with a space between them " ". For example: =CONCATENATE("Hello", " ", "World!"). Add a space after the Text argument.


1 Answers

String.concat "\n" [str1; str2 ... strn]

works fine. The problem is that you used '\n', which is a character literal, not a string. Example:

# String.concat '\n' ["abc"; "123"];;
Error: This expression has type char but an expression was expected of type
     string
# String.concat "\n" ["abc"; "123"];;
- : string = "abc\n123"
like image 59
Niklas B. Avatar answered Sep 18 '22 13:09

Niklas B.