SoFunction
Updated on 2025-03-09

C# replaces the special characters in the middle of the privacy information (bank account, ID number) with *

/// <summary>
/// Replace the middle part of the string with special characters/// </summary>
/// <param name="value">String that needs to be replaced</param>/// <param name="startLen">Previous reserved length</param>/// <param name="endLen">Tail retained length</param>/// <param name="replaceChar">Special Character</param>/// <returns>String replaced by special characters</returns>private static string ReplaceWithSpecialChar(string value, int startLen = 4, int endLen = 4, char specialChar = '*')
{
 try
 {
  int lenth =  - startLen - endLen;
  string replaceStr = (startLen, lenth);
  string specialStr = ;
  for (int i = 0; i &lt; ; i++)
  {
   specialStr += specialChar;
  }
  value = (replaceStr, specialStr);
 }
 catch (Exception)
 {
  throw;
 }
 return value;
}

The renderings are shown as follows:

Copy the codeThe code is as follows:

ReplaceWithSpecialChar("Ke Xiaodai", 1, 0,'*') -->Result: Ke *dai
ReplaceWithSpecialChar("622212345678485") -->Result: 6222*******8485
ReplaceWithSpecialChar("622212345678485", 4 , 4 , '*') -->Result: 6222*******8485

Note: If the incoming startLen/endLen exceeds the string length, a subscript out-of-bounds exception will be thrown.

C# implements parameter privacy code

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
//Import custom class libraryusing _3Layer.;
using _3Layer.;
using ;
namespace 
public class RenderingXML : 
{
/// &lt;summary&gt;
/// Pre-generate XML data source from the database/// &lt;/summary&gt;
private void PreRenderXML()
{
string strSQL = "selectStatements omitted........................;
();
RenderingXml="&lt;?xml version='1.0' 
encoding='gb2312'?&gt;\r\n";
RenderingXml+="&lt;xml&gt;\r\n";
try
{ 
 myDR 
= (SqlDataReader)( strSQL );
while(())
{
RenderingXml+="&lt;TreeNode id='"+myDR["BoardID"]+"'&gt;\r\n";
RenderingXml+="&lt;NodeText&gt;"+myDR["BoardName"]+"&lt;/NodeText&gt;\r\n";
RenderingXml+="&lt;title&gt;"+myDR["Title"]+"&lt;/title&gt;\r\n";
RenderingXml+="&lt;NodeUrl&gt;"+EncodeHTML
( EncodeParameter( myDR["Link"].ToString() ) )+"&lt;/NodeUrl&gt;\r\n";
RenderingXml+="&lt;child&gt;"+myDR["children"]+"&lt;/child&gt;\r\n";
RenderingXml+="&lt;target&gt;"+myDR["Target"]+"&lt;/target&gt;\r\n";
RenderingXml+="&lt;/TreeNode&gt;\r\n";
}
}
catch( ee)
{
return ;
}
finally
{
() ; 
}
RenderingXml+="&lt;/xml&gt;";
byte[] bytResult = ( RenderingXml ) ;
 = "text/xml" ;
( bytResult ) ;
} 
/// &lt;summary&gt;
/// Description: Encrypted path parameters/// &lt;/summary&gt;
/// &lt;param name="sourParameter"&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private string EncodeParameter( string sourParameter )
{
string startString =  ;
string endString =  ; 
StringBuilder destParameter = new StringBuilder() ;
if( sourParameter == null || ("") )
{
(  ).ToString() ;
}
else
{
//Start analyze the characters in the pathif( ("?")&lt;0 )
{
( sourParameter ).ToString() ;
}
else
{
//Separate the path with ?string[] paramPath = ( new char[]{'?'} ) ;
startString = paramPath[0].ToString() ;
endString = paramPath[1].ToString() ;
//Start analyze the & characters in the pathif(("&amp;")&lt;0)
{
//There is only one parameter, split with = sign, and directly encrypt NameValue with Desstring[] paramNameValue = ( new char[]{'='} ) ;
string paramName = 
( paramNameValue[0].ToString() ,myDESKey ) ;
string paramValue = 
( paramNameValue[1].ToString() ,myDESKey ) ;
( startString ).Append("?").
Append( paramName ).Append("=").Append( paramValue ) ;
}
else
{
//There are multiple parameters, and the path after the ? number is divided by the & number is the & numberstring[] paramJoin = ( new char[]{'&amp;'} ) ;
( startString ).Append("?").
Append( EncoderNameValue( paramJoin ) ) .ToString() ;
}
}
}
return () ;
} 
/// &lt;summary&gt;
/// Description: NameValue parameter in the encrypted path/// &lt;/summary&gt;
/// &lt;param name="sourNameValue"&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private string EncoderNameValue( string[] sourNameValue )
{
string[] paramNameValue ;
string paramName ;
string paramValue ;
StringBuilder sb = new StringBuilder() ;
for( int i = 0 ; i &lt;= -1 ; i++ )
{
//Segment each NameValue parameter with = numberparamNameValue = sourNameValue[i].Split( new char[]{'='} ) ;
//Start encryption of NameValueparamName = ( paramNameValue[0].ToString() ,myDESKey ) ;
paramValue = ( paramNameValue[1].ToString() ,myDESKey ) ;
//Storage the encrypted path string( paramName ).Append("=").Append( paramValue ) ;
//Is the last NameValue parameter? If it is not added to the Lucuiif( i&lt; )
{
("&amp;") ;
}
}
return () ;
}
} 

The above content is all the content of C# replacing the special characters in the middle of the privacy information (bank account, ID number) with *. I hope everyone likes it.