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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Automate
Answered

Replace a string

(0) ShareShare
ReportReport
Posted on by 7

Hi everyone, 

 

I have a string variable with the values "36.43.40.42.38.419.37.45." and I want to replace the last value ".45." the last value is always between dots.

 

How can I do that

I have the same question (0)
  • NathanAlvares24 Profile Picture
    1,714 Moderator on at

    Hi @Diego_Marquez !

     

    You can try using scripting languages like Python or Powershell as it a lot faster.

    Here is one flow:

    NathanAlvares24_0-1719437552543.png

     

    You can choose either Powershell or Python as both give the same result.

    Powershell code:

     

     

     

    # Original string
    $originalString = "%String%"
    
    # Split the string by the dot
    $splitArray = $originalString -split "\."
    
    # Remove the last empty element if it exists
    if ($splitArray[-1] -eq "") {
     $splitArray = $splitArray[0..($splitArray.Length - 2)]
    }
    
    # Remove the last value
    $splitArray = $splitArray[0..($splitArray.Length - 2)]
    
    # Get the second last value and append the new value
    $newValue = "NEWVALUE"
    $splitArray[-1] = $splitArray[-1] + $newValue
    
    # Join the array back into a string
    $newString = $splitArray -join "."
    
    # Output the new string
    $newString

     

     

     

     

    Python code:

     

     

     

    # Original string
    original_string = "%String%"
    
    # Split the string by the dot
    split_list = original_string.split(".")
    
    # Remove the last empty element if it exists
    if split_list[-1] == "":
     split_list = split_list[:-1]
    
    # Remove the last value
    split_list = split_list[:-1]
    
    # Get the second last value and append the new value
    new_value = "NEWVALUE"
    split_list[-1] = split_list[-1] + new_value
    
    # Join the list back into a string
    new_string = ".".join(split_list)
    
    # Output the new string
    print(new_string)

     

     

     

     

    Full code of the flow (just copy and paste into your flow):

     

     

     

    SET String TO $'''36.43.40.42.38.419.37.45.'''
    @@copilotGeneratedAction: 'False'
    Scripting.RunPowershellScript.RunPowershellScript Script: $'''# Original string
    $originalString = \"%String%\"
    
    # Split the string by the dot
    $splitArray = $originalString -split \"\\.\"
    
    # Remove the last empty element if it exists
    if ($splitArray[-1] -eq \"\") {
     $splitArray = $splitArray[0..($splitArray.Length - 2)]
    }
    
    # Remove the last value
    $splitArray = $splitArray[0..($splitArray.Length - 2)]
    
    # Get the second last value and append the new value
    $newValue = \"NEWVALUE\"
    $splitArray[-1] = $splitArray[-1] + $newValue
    
    # Join the array back into a string
    $newString = $splitArray -join \".\"
    
    # Output the new string
    $newString''' ScriptOutput=> PowershellOutput ScriptError=> ScriptError
    @@copilotGeneratedAction: 'False'
    Scripting.RunPythonScript PythonCode: $'''# Original string
    original_string = \"%String%\"
    
    # Split the string by the dot
    split_list = original_string.split(\".\")
    
    # Remove the last empty element if it exists
    if split_list[-1] == \"\":
     split_list = split_list[:-1]
    
    # Remove the last value
    split_list = split_list[:-1]
    
    # Get the second last value and append the new value
    new_value = \"NEWVALUE\"
    split_list[-1] = split_list[-1] + new_value
    
    # Join the list back into a string
    new_string = \".\".join(split_list)
    
    # Output the new string
    print(new_string)''' PythonVersion: System.PythonVersion.Python3 ScriptOutput=> PythonScriptOutput ScriptError=> ScriptError

     

     

     

     

    Output:

    Before:

    NathanAlvares24_1-1719437739509.png

     

    After:

    NathanAlvares24_2-1719437753968.png

     

    NathanAlvares24_3-1719437784189.png

     

    So it will replace ".45." which comes in format as you said some data between the dots and at the end always with any value you specify in the script. I replaced that with "NEWVALUE"

     

    Other input:

    Before:

    NathanAlvares24_4-1719437990377.png

     

    After:

    NathanAlvares24_5-1719438000952.png

     

    I hope this helps.

     

  • Verified answer
    Deenuji_Loganathan_ Profile Picture
    6,255 Moderator on at

    @Diego_Marquez 

     

    There's no need to overthink this logic.

     

    Here you go with simple one:

    Deenuji_1-1719461066516.png

     

     

    Code:

    SET Decimal TO $'''36.43.40.42.38.419.37.45.'''
    Variables.CreateNewList List=> DecimalList
    Variables.AddItemToList Item: $'''36.43.40.42.38.419.37.45.''' List: DecimalList
    Variables.AddItemToList Item: $'''12.34.56.78.90.123.45.67.''' List: DecimalList
    Variables.AddItemToList Item: $'''23.45.67.89.12.345.67.89.''' List: DecimalList
    Variables.AddItemToList Item: $'''78.56.34.12.90.234.56.78.''' List: DecimalList
    LOOP FOREACH CurrentItem IN DecimalList
     Text.Replace Text: CurrentItem TextToFind: $'''\\.(\\d+)\\.(?=[^.]*$)''' IsRegEx: True IgnoreCase: True ReplaceWith: $'''%''%''' ActivateEscapeSequences: True Result=> Replaced
     Display.ShowMessageDialog.ShowMessage Title: $'''Info''' Message: $'''Replaced number : %Replaced%''' Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: False ButtonPressed=> ButtonPressed
    END

     

    New to Power automate? How to copy and paste the above code into your power automate desktop?

    Deenuji_2-1719461138062.gif

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

  • Diego_Marquez Profile Picture
    7 on at

    Thanks you so much Deenuji it works perfectly

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 862

#2
Valantis Profile Picture

Valantis 738

#3
Haque Profile Picture

Haque 553

Last 30 days Overall leaderboard