I have a string where I need to replace a particular string, my string is as follows
string s = @"-Username ABC **-Password XYZ** -Address MNO";
string[] s1 = s.Split('-');
var newList = s1.Where(s2 =>s2.Contains("Password"));
-Password XYZ
-Password ****
Test
[TestCase("-Username ABC -Password XYZ -Address MNO", "-Username ABC -Password *** -Address MNO")]
public void Test(string toTest, string expected )
{
Assert.AreEqual(expected, DestroyPassword(toTest));
}
Code
public static string DestroyPassword(string str )
{
var r = new Regex("-Password .*? ");//? in .net means not greedy
return r.Replace(str, "-Password *** ");
}