/// <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 < ; 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 : { /// <summary> /// Pre-generate XML data source from the database/// </summary> private void PreRenderXML() { string strSQL = "selectStatements omitted........................; (); RenderingXml="<?xml version='1.0' encoding='gb2312'?>\r\n"; RenderingXml+="<xml>\r\n"; try { myDR = (SqlDataReader)( strSQL ); while(()) { RenderingXml+="<TreeNode id='"+myDR["BoardID"]+"'>\r\n"; RenderingXml+="<NodeText>"+myDR["BoardName"]+"</NodeText>\r\n"; RenderingXml+="<title>"+myDR["Title"]+"</title>\r\n"; RenderingXml+="<NodeUrl>"+EncodeHTML ( EncodeParameter( myDR["Link"].ToString() ) )+"</NodeUrl>\r\n"; RenderingXml+="<child>"+myDR["children"]+"</child>\r\n"; RenderingXml+="<target>"+myDR["Target"]+"</target>\r\n"; RenderingXml+="</TreeNode>\r\n"; } } catch( ee) { return ; } finally { () ; } RenderingXml+="</xml>"; byte[] bytResult = ( RenderingXml ) ; = "text/xml" ; ( bytResult ) ; } /// <summary> /// Description: Encrypted path parameters/// </summary> /// <param name="sourParameter"></param> /// <returns></returns> 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( ("?")<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(("&")<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[]{'&'} ) ; ( startString ).Append("?"). Append( EncoderNameValue( paramJoin ) ) .ToString() ; } } } return () ; } /// <summary> /// Description: NameValue parameter in the encrypted path/// </summary> /// <param name="sourNameValue"></param> /// <returns></returns> private string EncoderNameValue( string[] sourNameValue ) { string[] paramNameValue ; string paramName ; string paramValue ; StringBuilder sb = new StringBuilder() ; for( int i = 0 ; i <= -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< ) { ("&") ; } } 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.