Insert Hyphen after every 4th character in input.

Hi All,

 

I have a requirement, have to separate the string with a Hyphen(-) after every 4th character entered by the use, also it should not count space as string and should ignore it.

  Discussion posts and replies are publicly visible

  • If you have the regular expressions plug-in, you could try...

     

    load(
      local!example,
      a!textField(
        value: local!example,
        saveInto: a!save(
          local!example,
          joinarray(regexallmatches("....|.*", stripwith(save!value, " -")), "-")
        )
      )
    )

  • 0
    Certified Lead Developer
    in reply to normanc

    insert("2345987611112222", "-", {5,9,13,})  Run this in "Try it now" box on any function documentation page.

    This should be a good start. You can use stripwith() to remove the spaces, or anything else you don't want. The stripwith function with your text input as it's first parameter will replace "2345987611112222"

     

    Advantages over others:  You don't need a plugin and it's only 1 line of code.

  • You can use regular expression plug-in. Giving the 2 examples here, please check which satisfies your requirement

    string= "The local variable to use when evaluating the given expression."
    1) regexinsertmatchmarkers("\S{4}", stripwith(string, " "), "", "-", false)
    Returns : "Thel-ocal-vari-able-tous-ewhe-neva-luat-ingt-hegi-vene-xpre-ssio-n."

    2) regexinsertmatchmarkers("\S \S{3}|\S{2} \S{2}|\S{3} \S|\S{4}", local!string, "", "-", false)
    Returns: "The l-ocal- vari-able- to us-e whe-n eva-luat-ing t-he gi-ven e-xpre-ssio-n."

    Thanks,
    Dipak Ingle
  • 0
    Certified Lead Developer

    Hi Kumar, 

    You can create an expression rule with below code and use it: 

    with(
      local!insertIndex: 4, /*"Insert Hyphen after every 4th character in input."*/
      if(isnull(ri!text), null, 
        joinarray(
        char(
          insert(
            code(
              ri!text
            ),
            45, /* To Insert Hyphen */
            (
              (
                enumerate(
                  quotient(
                    len(
                      ri!text
                    ),
                    local!insertIndex
                  )
                ) + 1
              ) * local!insertIndex
            ) + 1
          )
        )
       )
      )
     )

  • 0
    Certified Lead Developer
    Small correction on vimalkumars (vimal) code. If text is up to 4 character length you don't want hyphen(-), right? Use below code.
    Thanks.


    with(
    local!insertIndex: 4, /*"Insert Hyphen after every 4th character in input."*/
    if(isnull(ri!text), null,
    if(len(ri!text)<5,ri!text, joinarray(
    char(
    insert(
    code(
    ri!text
    ),
    45, /* To Insert Hyphen */
    (
    (
    enumerate(
    quotient(
    len(
    ri!text
    ),
    local!insertIndex
    )
    ) + 1
    ) * local!insertIndex
    ) + 1
    )
    )
    )
    )
    )
    )