web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Replace '#' with random number in given string

(0) ShareShare
ReportReport
Posted on by 2,110

Hi Techies,

I need to generate a string with specified format where numbers are denoted with character '#'. 

For example= AB#######123####D should generate AB+(seven digit random number)+123+(four digit random number)+D.

i.e. AB87934611232903D.

I have written a logic which works only if there is a single combination of # in given format. e.g. AB#######123D.


generatedString = strReplace(_numberFormat, strKeep(_numberFormat,'#'), customSysStringGenerator::randomDec(strLen(strKeep(_numberFormat, "#"))));

It seems I need to traverse each character and replace # with random number, but is there any way to make it with more elegant method, (may be regular expressions)

Any help is appreciated.

Thanks,

Sangram Shinde

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at
    RE: Replace '#' with random number in given string

    Hi Sangram,

    what's the business requirement behind this?

    I think you might want to split this into two clearly separate parts:

    1) Generate random numbers (perhaps with custom length)

    2) Replace parts of a string with another string (which comes from your random generator)

    Depending on the business requirement, the way how you implement 1 can be very critical. 2 should be trivial.

  • Sangram Shinde Profile Picture
    2,110 on at
    RE: Replace '#' with random number in given string

    Thank you for reply Nikolaos,

    The requirement behind this is to generate a specific ID to let them identify their orders, so depending on condition they can give any format with # at any place and any number of times in a string.

    e.g.

    AB#####C1#88#

    #123RC####

    So it seems I need to replace parts of string with numbers as u said.

  • nmaenpaa Profile Picture
    101,160 Moderator on at
    RE: Replace '#' with random number in given string

    Why would you not use a simple number sequence to identify orders?

  • Verified answer
    ergun sahin Profile Picture
    8,824 Moderator on at
    RE: Replace '#' with random number in given string

    I think you can do something like this (the code looked a bit strange even to me :) but I just woke up. Improvements are possible but it works)

        str generatedString  = "AB#######123####D";
        System.Random rnd = new System.Random();
        int rndValue;
        int changePoint = strFind(generatedString ,"#",1,strLen(generatedString));
        ;
        while(changePoint)
        {
            rndValue = rnd.Next(9);       
            generatedString = subStr(generatedString,1,changePoint-1)   int2str(rndValue)   subStr(generatedString,changePoint 1,strLen(generatedString)-changePoint) ;       
            changePoint = strFind(generatedString ,"#",1,strLen(generatedString ));    
        }
       
        info(generatedString);

  • Sangram Shinde Profile Picture
    2,110 on at
    RE: Replace '#' with random number in given string

    Perfect Ergun, it's working. Thank you!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 1,882

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 794 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 525 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans