Using string manipulation is not the reliable solution I would say. Use RegEx approach for this type of problem statement.
Follow this mega post to refer for RegEx.
Regex help tutorial MEGAPOST – Making your first Regex post, Reusable Regex Patterns, Regex Troubleshooting, Sample Workflow and more - News / Tutorials - UiPath Community Forum
Thanks,
Ashok