Wednesday, September 3, 2008

Replace Strings in C#

If i have a String like this:

LDAP://CN=arth@test.com,OU=Consolidatedmessenger,OU=Hosting,DC=fabrikam,DC=com

and i want to remove a certain portion from this and want my string finally as :

LDAP://OU=test,OU=Hosting,DC=fabrikam,DC=com


This is how we will do :

string strVal = "LDAP://CN=arth@test.com,OU=Consolidatedmessenger,OU=Hosting,DC=fabrikam,DC=com";
string strToReplace = "";

int leng = strVal.IndexOf(",") - (strVal.IndexOf("/") + 1);

strToReplace = strVal.Substring(strVal.IndexOf("//") + 2, leng);

strVal = strVal.Replace(strToReplace, "");



Thanks ,
Happy Coding..!!
Nitin Sharma