Skip to main content

Notifications

Power Apps - Building Power Apps
Answered

Insert character in a string field

(0) ShareShare
ReportReport
Posted on by 636

I have a table/collection in PowerApps where the strings are formatted like: WW031-231, DT031-222, etc. I would like to convert these by inserting a hyphen after the first two characters, to produce WW-031-231, DT-031-222, etc.

What is the simplest way to do this? Some combination of replace, substitute, split, LEN?

 

Thanks.

  • timl Profile Picture
    timl 34,352 on at
    Re: Insert character in a string field

    Hi @Runner55552 

    Thanks - that's correct - AddColumns doesn't add columns to an existing collection - it's a function that returns a table with the additional columns you specify. Therefore, you would need to Collect to a new collection, or you can do as I did in the screenshot and set the Items property of a gallery, datatable or control directly to the call to AddColumns.

  • 55552 Profile Picture
    55552 636 on at
    Re: Insert character in a string field

    Thanks! Just accepted yours also. I did not know you could accept more than one solution. Also, I just tried your solution in my app and it worked well. Since some of my codes have a different format (132-2344 or 132-234), without letters at the front, I needed to wrap an If statement around the With code to check the total length of the string. If >= 9 characters, implement the With statement, else keep the original text.

    I have also noticed that I cannot use AddColumns on an existing collection. It appears to add columns to a collection, I need to create a new collection. Is that correct?

    Runner55552_0-1709217724443.png

     

  • timl Profile Picture
    timl 34,352 on at
    Re: Insert character in a string field

    Thanks @Runner55552 - I'm glad that works for you. Please note that it's possible to select multiple posts as solutions in situations like this.

  • 55552 Profile Picture
    55552 636 on at
    Re: Insert character in a string field

    Thanks. This looks like it would work. Accepted the first suggestion above from @SpongYe as the solution, partly because it came in first, but both solutions appear to work.

  • Verified answer
    timl Profile Picture
    timl 34,352 on at
    Re: Insert character in a string field

    Hi @Runner55552 

    If you're looking for some formula to apply this transformation to a collection, let's say you have a collection that looks like this:

    ClearCollect(colData,
     {Code: "WW031-231"}, {Code: "DT031-222"}
    )

    You can call the AddColumns function to apply this transformation to the collection.

    AddColumns(colData, 
     "NewCode", 
     With({splitStr:Split(Code, "-")}, 
     Left(Code, 2) & "-" & 
     Mid( First(splitStr).Value,3, Len(First(splitStr).Value)-2) & "-" &
     Last(splitStr).Value 
     )
    )

     

    timl_0-1708647193870.png

     

  • Verified answer
    SpongYe Profile Picture
    SpongYe 5,411 on at
    Re: Insert character in a string field

    Hi @Runner55552 

     

    I used Set but use what you are more comfortable with:

    Set(OriginalString, "WW031-231");
    Set(FirstString, Left(OriginalString, 2));
    Set(SeconString, Mid(OriginalString, 3, 3)); 
    Set(ThirdString, Right(OriginalString, Len(OriginalString) - 6));
    Set(
     CombinedString, 
     Concatenate(FirstString, "-", SeconString, "-", ThirdString)
    )
    
    

    SpongYe_0-1708643097751.png

     

    If you have any questions or feedback, please let me know. Have a great day! 😊

    -----------------------
    PowerYsa Power Platform Enthusiast [LinkedIn] | [Youtube]

    I love to share my knowledge and learn from others. If you find my posts helpful, please give them a thumbs up 👍 or mark them as a solution ✔️. You can also check out my [@PowerYSA] for some cool solutions and insights. Feel free to connect with me on any of the platforms above. Cheers! 🍻

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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,567

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,907

Leaderboard