3

I have a pipe-separated string

www.test.com | www.test1.com

I want to get just the first string before the pipe that is www.test.com.

How can I do this in the scriban template?

2

Scriban has a split function on strings: https://github.com/scriban/scriban/blob/master/doc/builtins.md#stringsplit

string.split <text> <match>

The split function takes on a substring as a parameter. The substring is used as a delimiter to divide a string into an array. You can output different parts of an array using array functions.

Also check the whitespace control if needed (I do see spaces around your pipes): https://github.com/scriban/scriban/blob/master/doc/language.md#14-whitespace-control

  • The greedy mode using the character - (e.g {{- or -}}), removes any whitespace, including newlines
  • The non greedy mode using the character ~
    • Using a {{~ will remove any whitespace before but will stop on the first newline without including it
    • Using a ~}} will remove any whitespace after including the first newline but will stop after

Working with arrays is explained at https://github.com/scriban/scriban/blob/master/doc/builtins.md#array-functions. You could use array.first <list> to get the first element.

Update

Try: {{ i_item.URLList | string.split "|" | array.first }}

That should do it..

4
  • i tried the following. I can get the urls in the list but cannot get the first url from array {{ for urls in i_item.URLList | string.split ' ' ~}} {{ end }} {{ urls }} {{ urls | array.first }} – user2980 Mar 25 at 9:29
  • Why are you using a for loop? You don't have to loop through your split items.. – Gatogordo Mar 25 at 9:40
  • because the field i_item.URLList has values like www.test.com | www.test1.com . So first I need to convert it into the array and then get the first array element? – user2980 Mar 25 at 9:43
  • Added to the answer.. should work like that (as Alina also mentioned) – Gatogordo Mar 25 at 10:29
2

Maybe you can use the string split (by '|') function that will get you the array: [www.test.com, www.test1.com]. Then you can read first element of the array using array first function. Example:

{{ for word in "Hi, how are you today?" | string.split ' ' ~}}
{{ word }}
{{ end ~}}

output

Hi,

how

are

you

today?
4
  • thanks for the answer. How can I read the first element? – user2980 Mar 25 at 9:19
  • example: {{ [4, 5, 6] | array.first }} – Alina Fodor Mar 25 at 9:20
  • i tried the following. I can get the urls in the list but cannot get the first url from array {{ for urls in i_item.URLList | string.split ' ' ~}} {{ end }} {{ urls }} {{ urls | array.first }} – user2980 Mar 25 at 9:29
  • can you try: {{ "url1|url2" | string.split '|' | array.first }} where "url1|url2" is i_item.URLList . You can test code here: scribanonline.azurewebsites.net – Alina Fodor Mar 25 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy