I have a card number as a string, for example:
string ClsCommon.str_CardNumbe r = \"3456123434561234\";
The length of this card number can
How about replacing a specific matched group using Regex :
string cardNumber = "3456123434561234";
var pattern = "^(.{6})(.+)(.{4})$";
var maskedNumber = Regex.Replace(cardNumber, pattern, (match) =>
{
return Regex.Replace(String.Format("{0}{1}{2}",
match.Groups[1].Value, // the first 6 digits
new String('X', match.Groups[2].Value.Length), // X times the 'X' char
match.Groups[3].Value) /*the last 4 digits*/,".{4}", "$0 "); //finally add a separator every 4 char
});