want to convert "indiaChinaFrance" to "India China France"

Certified Senior Developer

want to display dynamic field name on screen but with updating initial as capital and adding one space in between.Field name always start with small caps

eg. "indiaChinaFrance" to "India China France"

eg. abcDefXyz to "Abc Def Xyz"

  Discussion posts and replies are publicly visible

Parents
  • I'm sure there are easier ways (awaits others to gleefully provide simpler solutions) but here's my offering:

    a!localVariables(
      local!items: { "A", "B", "C", "D", "E", "F" },
      local!splitAttribute: fn!reduce(fn!split, ri!attributeName, local!items),
      a!forEach(
        items: local!splitAttribute,
        expression: if(
          fv!isFirst,
          fn!proper(fv!item),
          fn!concat(
            ri!attributeName[fn!find(fv!item, ri!attributeName, 1) - 1],
            fv!item
          )
        )
      )
    )

    The idea is to have the full set of characters that you want to use as delimiters in the string you want to process (I've only provide A-F as an example).

    You then split the incoming string based upon that list. Unfortunately (j this instance) the split function excludes the value you're splitting, so you up wiht a list where the first item is whole but the remaining items are missing their first character. 

    The aim of the remainder of the code is to re-append the missing character to each item in the list generated by the split processing.

  • 0
    Certified Lead Developer
    in reply to Stewart Burchell

    Assuming we'll always (and only) split on uppercase characters versus lowercase characters, this seems like a good place to (ab)use code() and char() conversions...

  • 0
    Certified Lead Developer
    in reply to Mike Schmitt

    a!localVariables(
      local!lowerCaseA: code("a"),
      
      local!inputString: "indiaChinaFrance",
      
      local!codeArray: code(local!inputString),
      
      local!modifiedString: concat(a!forEach(
        local!codeArray,
        if(
          fv!item < local!lowerCaseA,
          " " & char(fv!item),
          if(
            fv!isFirst,
            upper(char(fv!item)),
            char(fv!item)
          )
        )
      )),
      
      local!modifiedString
    )

  • Yup, I think this is a more robust solution than mine...although if you ONLY want the delimiters to be Capital Letters then you'd need to exclude all those characters who have letters < A

  • 0
    Certified Lead Developer
    in reply to Stewart Burchell

    yeah i agree, in a more complete implementation the if() logic should check that the current character is in the range of uppercase alphabetical letters, whereas in my quick-fix approach i just made checked that the current character's ascii code is lower than the lowest lowercase (since the ascii codes for uppercase characters are lower than the ones for lowercase, for anyone unfamiliar).

    Attached is the less lazy way:

    a!localVariables(
      /*local!lowerCaseA: code("a"),*/
      local!upperCaseA: code("A"),
      local!upperCaseZ: code("Z"),
      
      local!inputString: "indiaChinaFrance",
      
      local!codeArray: code(local!inputString),
      
      local!modifiedString: concat(a!forEach(
        local!codeArray,
        if(
          and(
            fv!item >= local!upperCaseA,
            fv!item <= local!upperCaseZ
          ),
          " " & char(fv!item),
          if(
            fv!isFirst,
            upper(char(fv!item)),
            char(fv!item)
          )
        )
      )),
      
      local!modifiedString
    )

Reply
  • 0
    Certified Lead Developer
    in reply to Stewart Burchell

    yeah i agree, in a more complete implementation the if() logic should check that the current character is in the range of uppercase alphabetical letters, whereas in my quick-fix approach i just made checked that the current character's ascii code is lower than the lowest lowercase (since the ascii codes for uppercase characters are lower than the ones for lowercase, for anyone unfamiliar).

    Attached is the less lazy way:

    a!localVariables(
      /*local!lowerCaseA: code("a"),*/
      local!upperCaseA: code("A"),
      local!upperCaseZ: code("Z"),
      
      local!inputString: "indiaChinaFrance",
      
      local!codeArray: code(local!inputString),
      
      local!modifiedString: concat(a!forEach(
        local!codeArray,
        if(
          and(
            fv!item >= local!upperCaseA,
            fv!item <= local!upperCaseZ
          ),
          " " & char(fv!item),
          if(
            fv!isFirst,
            upper(char(fv!item)),
            char(fv!item)
          )
        )
      )),
      
      local!modifiedString
    )

Children
No Data