I need to remove a specific value from middle of a string and only its first occurrence from end of the string e.g.
Url : https://something/ABCD/EFGH/IJKL/**ABCD**?id=1234567910
In above url string I need to replace last "ABCD" with "NEW" as below:
Url : https://something/ABCD/EFGH/IJKL/**NEW**?id=1234567910
currently how I'm doing this is
using System;
namespace StringReplaceSample
{
public class Program
{
public static void Main(string[] args)
{
string pageid = "?id=1234567910";//its something i can retrive or get
string example = "https://something/ABCD/EFGH/IJKL/ABCD?id=1234567910";
String[] breakApart = example.Split('/');
var exampleTrimmedlastValue = breakApart[breakApart.Length-1];
var exampleReplace = example.Replace(exampleTrimmedlastValue,"NEW");
var exampleTrimmed = exampleReplace+pageid;
Console.WriteLine("Original string:" +example);
Console.WriteLine("Trimmed string:"+exampleTrimmed);
}
}
}
but I don't find this very efficient and its huge can someone suggest any simpler way to do this