How to replace first occurrence of number in string by particular string

For example 
String :-
"1 - ACE-563-LTFU - Original Received Date - 2/24/2019 - 123-ABCLKDEFR"

Replace 1 with "ABC"

Return as :-
"ABC - ACE-563-LTFU - Original Received Date - 2/24/2019 - 123-ABCLKDEFR"



  Discussion posts and replies are publicly visible

  • So, it depends on the exact requirements: will the number always be a 1-digit integer in position 1 of the string? Or the first number of any length to appear at any position within the string? The former will be one level of complexity to deliver, the latter another level of complexity. In either case, you need to break down the problem into a set of smaller problems and solve each one at a time.

    Let's assume the first scenario: that the number will always be a single digit integer at position 1. You can use the fn!replace() to solve this easily. Assuming the string is passed to a rule in ri!myString:

    fn!replace(
    ri!myString, /* old_text */
    1, /* start_num */
    1, /* num_chars */
    "ABC" /* new_text */
    )

    This is now a very specific concrete solution and will only ever work for the use case described.

    If the first number can appear ANYWHERE in the string, and can be on ANY length, then we would need to perform more operations to determine the values of two different variables that will replace both of the hard-coded values of start_num and num_chars in the above example. Either way, we'd also probably want to replace the hard-coded value of new_text with either a constant (for maintenance purposes), or another rule input to make the rule generic.
  • 0
    Certified Lead Developer

    Find() function to find the start location of the first instance, if you want to replace the first 1 to appear in your text with "ABC". Then replace function to replace it with "ABC"

    Your code might well be replace(ri!yourText, find("1", ri!yourText), 1, "ABC") third parameter of replace being 1 because you only want to remove 1 character from the original text.