How to concat two text for a Text?

How to concat two to Text? Eg: text1 = “aa”;text2=“bb” .How to get text3 = text1+text2= “aabb”

2 Likes

You can use <>, so text1 <> text2. It is actually a bit more general than just Text and has the type:

(<>) : Semigroup a => a -> a -> a

Text is an instance of Semigroup but so is for example [a] so you can also use it to concatenate lists.

3 Likes

To illustrate, using daml repl:

daml> let text1 = "aa"
aa
daml> let text2 = "bb"
bb
daml> let text3 = text1 <> text2
aabb
daml> [1, 2, 3] <> [4, 5, 6]
[1,2,3,4,5,6]
daml> "hello" <> "world"
"helloworld"
daml> 

or, as a full standalone project:

module Main where

import Daml.Script

setup : Script ()
setup = script do
  let text1 = "aa"
  let text2 = "bb"

  assert $ "aabb" == (text1 <> text2)
4 Likes

Thanks for your help. It is great,While how to get those grammar document which not describe in “daml.com” , i had no way to find the answer by myself .

2 Likes

Our documentation is well integrated with a Daml-specific instance of Hoogle, which allows you to look for functions with specific signatures.

In your case, you would be looking for Text -> Text -> Text and the result would be https://docs.daml.com/search.html?query=Text%20-%3E%20Text%20-%3E%20Text.

Admittedly, <> is a very generic operation and the result is probably a bit hidden to newcomers. I hope this may still help you in the future.

EDIT: unfortunately the link I originally posted don’t work, please copy and paste the URLs in your browser. Apologies for the incovenience.

4 Likes