<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-M74D8PB" height="0" width="0" style="display:none;visibility:hidden">
Loading
Skip to NavigationSkip to Main Content
0D51Y000081mL8pSAEOkta Classic EngineUniversal DirectoryAnswered2024-04-15T10:20:26.000Z2020-03-10T13:07:27.000Z2020-03-11T07:22:33.000Z

sp55m (sp55m) asked a question.

Okta expression language to pull last four numbers from a string

I have an incoming attribute with variable lengths and no common key (such as a @) and I only need the last four digits from the field. Any whizzes out there know the OEL command to count from the right instead of left?


  • GabrielL.85945 (Customer)

    Expression:

    String.substring(appuser.stringAttribute, String.len(appuser.stringAttribute)-4, String.len(appuser.stringAttribute))

     

    Summary

    You'll need to change "appuser.stringAttribute" to be the actual attribute you have in your environment in all three places. Basically were using the String.substring function to trim the string to a shorter length. This function needs three inputs: the attribute, a starting character in the string, and an ending character in the string. The function will keep only the characters from where you specify to start and finish. The problem you explained is that we don't know the exact character count in the string. So what we do is use the String.len function to determine how many characters are in the string. Because you want the last four characters, we can use this as the ending character to give to the String.substring function. Now, we just need the starting character. Since you just want the last four, we use the String.len function again and just subtract four from it. This gives us the starting character in the string.

     

    Expression Breakdown

    Attribute value will equal "HelloWorld".

    1. Original Expression: String.substring(appuser.stringAttribute, String.len(appuser.stringAttribute)-4, String.len(appuser.stringAttribute))
    2. Change attribute name to attribute value: String.substring("HelloWorld", String.len("HelloWorld")-4, String.len("HelloWorld"))
    3. Evaluate the String.len function: String.substring("HelloWorld", 10-4, 10)
    4. Perform subtraction: String.substring("HelloWorld", 6, 10)
    5. Evaluate the String.substring function: "orld"

     

     

    Examples Input/Ouput:

     

    Input: IAmAString

    Output: ring

     

    Input: 477807248411

    Output: 8411

     

    Documentation:

    https://developer.okta.com/docs/reference/okta-expression-language/#string-functions

     

    Note:

    • You can easily change the subtraction of four to another number and get a different character length. So if you found you needed the last six characters, just subtract six instead of four.
    • Be mindful of data types. The title said "string", but the example only mentioned "digits", which could be an attribute that's an integer.

     

    Expand Post
This question is closed.
Loading
Okta expression language to pull last four numbers from a string