Skip to content
Discussions/App Development/How to concat two text for a Text?Forum ↗

How to concat two text for a Text?

App Development5 posts610 views15 likesLast activity Jan 2021
HU
huixin_liuOP
Jan 2021

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

CO
cocreature
Jan 2021

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.

GA
Gary_Verhaegen
Jan 2021

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)
HU
huixin_liu
Jan 2021

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 .

ST
stefanobaghino-da
Jan 2021

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.

← Back to Discussions