Skip to main content
September 30, 2022
Question

Split a string

  • September 30, 2022
  • 4 replies
  • 0 views

Hi, I have a string in DB which is: "<key>PROJECT_CODE</key><style>BOLD</style><color>#EF0010</color>"

and I want your help to split this into different variables, like after splitting the above string 

var1 will have value = PROJECT_CODE,

var2 will have value = BOLD,

var3 will have value = #EF0010

Thanks in advance!

    4 replies

    September 30, 2022

    Try this.

    a!forEach(
      items: split(
        "PROJECT_CODE#EF0010",
        "><"
      ),
      expression: striphtml(
        if(
          fv!isFirst,
          concat(fv!item,">"),
          fv!isLast,
          concat("<",fv!item),
          concat("<" ,fv!item,">")
        )
      )
    )

    September 30, 2022

    Yes, it worked Thanks!

    mikes0011
    Brainy
    September 30, 2022

    I see there are some working implementations here already but I worry in some of those cases you could end up with some unintended side-effects, particularly if the data isn't very clean for some reason at any point.

    My question is, do you know the XML key names in advance?  Like, "key", "style", and "color", etc?  Will these vary widely, or always be exaclty the same?

    I'd think it might be the best idea to extract using the exact key names and go from there.  This would look more like 'extract(local!string, "<key>", "</key>")' - which would result in your output containing the exact string (without having to use additional functionality to remove the remnant XML characters) and protect you against dirty data (like anything containing miscellaneous "<" or ">" characters).