Copy codeThe Code is as follows: using System;
Using System. Collections;
Using System. Collections. Generic;
Using System. Text;
Using System. Text. RegularExpressions;
Using System. Security. Cryptography;
Using System. IO;
Using System. Text;
Namespace StringClass
{
Public class StringHelper
{
/// <Summary>
/// Remove all spaces in the string
/// </Summary>
/// <Param name = "_ str"> </param>
/// <Returns> </returns>
Public static string ReMoveBlank (string _ str)
{
String strTemp = "";
CharEnumerator CEnumerator = _ str. GetEnumerator ();
While (CEnumerator. MoveNext ())
{
Byte [] array = new byte [1];
Array = System. Text. Encoding. ASCII. GetBytes (CEnumerator. Current. ToString ());
Int asciicode = (short) (array [0]);
If (asciicode! = 32)
{
StrTemp + = CEnumerator. Current. ToString ();
}
}
Return strTemp;
}
/// <Summary>
/// Intercept the string and limit the length of the string, more than the given length +...
/// </Summary>
/// <Param name = "str"> string to be intercepted </param>
/// <Param name = "len"> the length of each line, more than the length of this line. </param>
/// <Param name = "max"> maximum output String Length </param>
/// <Returns> </returns>
Public static string CutStr (string str, int len, int max)
{
String s = "";
String sheng = "";
If (str. Length> max)
{
Str = str. Substring (0, max );
Sheng = "";
}
For (int I = 0; I <str. Length; I ++)
{
Int r = I % len;
Int last = (str. Length/len) * len;
If (I! = 0 & I <= last)
{
If (r = 0)
{
S + = str. Substring (I-len, len) + "<br> ";
}
}
Else if (I> last)
{
S + = str. Substring (I-1 );
Break;
}
}
Return s + sheng;
}
/// <Summary>
/// Intercept a string without specifying the length of the string
/// </Summary>
/// <Param name = "str"> string to be intercepted </param>
/// <Param name = "len"> the length of each line, more than the length of this line. </param>
/// <Returns> </returns>
Public static string CutStr (string str, int len)
{
String s = "";
For (int I = 0; I <str. Length; I ++)
{
Int r = I % len;
Int last = (str. Length/len) * len;
If (I! = 0 & I <= last)
{
If (r = 0)
{
S + = str. Substring (I-len, len) + "<br> ";
}
}
Else if (I> last)
{
S + = str. Substring (I-1 );
Break;
}
}
Return s;
}
Public static string PartSubString (string str, int len)
{
If (str. Length> len)
{
Return str. Substring (0, len) + "...";
}
Return str;
}
/// <Summary>
/// This method ensures that user input is not vicious
/// </Summary>
/// <Param name = "text"> input string </param>
/// <Param name = "maxLength"> maximum length </param>
/// <Returns> converted string </returns>
Public static string InputText (string text, int maxLength)
{
Text = text. Trim ();
If (string. IsNullOrEmpty (text ))
Return string. Empty;
If (text. Length> maxLength)
Text = text. Substring (0, maxLength );
Text = Regex. Replace (text, "[\ s] {2,}", ""); // two or more spaces
Text = Regex. replace (text, "(<[B | B] [r | R]/*>) + | (<[p | P] (. | \ n) *?>) "," \ N "); // <br>
Text = Regex. replace (text, "(\ s * & [n | N] [B | B] [s | S] [p | P]; \ s *) + ", "");//
Text = Regex. Replace (text, "<(. | \ n) *?> ", String. Empty); // any other tags
Text = text. Replace ("'","''");
Return text;
}
/// <Summary>
/// Convert uppercase to lowercase characters in a string
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public static string StringToLower (string str)
{
Char [] a = str. ToCharArray ();
String strTemp = "";
For (int I = 0; I <a. Length; I ++)
{
If (Convert. ToInt32 (a [I])> = 65 & Convert. ToInt32 (a [I]) <= 90)
StrTemp + = a [I]. ToString (). ToLower ();
Else
StrTemp + = a [I]. ToString ();
}
Return strTemp;
}
/// <Summary>
/// Encryption
/// </Summary>
/// <Param name = "str"> </param>
/// <Param name = "key"> must be an 8-Bit String </param>
/// <Returns> </returns>
Public static string Encode (string str, int keyIndex)
{
ArrayList alKey = new ArrayList ();
AlKey. Add ("BookT @ # +! NumBq2 ");
AlKey. Add ("MagaZine @ (21 & * ID5 ");
AlKey. Add ("ThesisDSHI}. _ Y ");
String key = alKey [keyIndex]. ToString ();
DESCryptoServiceProvider provider = new DESCryptoServiceProvider ();
Provider. Key = Encoding. ASCII. GetBytes (key. Substring (0, 8 ));
Provider. IV = Encoding. ASCII. GetBytes (key. Substring (0, 8 ));
Byte [] bytes = Encoding. GetEncoding ("GB2312"). GetBytes (str );
MemoryStream stream = new MemoryStream ();
CryptoStream stream2 = new CryptoStream (stream, provider. CreateEncryptor (), CryptoStreamMode. Write );
Stream2.Write (bytes, 0, bytes. Length );
Stream2.FlushFinalBlock ();
StringBuilder builder = new StringBuilder ();
Foreach (byte num in stream. ToArray ())
{
Builder. AppendFormat ("{0: X2}", num );
}
Stream. Close ();
Return builder. ToString ();
}
/// <Summary>
/// Des decrypts GB2312
/// </Summary>
/// <Param name = "str"> Desc string </param>
/// <Param name = "key"> Key, which must be 8 bits </param>
/// <Returns> </returns>
Public static string Decode (string str, int keyIndex)
{
ArrayList alKey = new ArrayList ();
AlKey. Add ("BookT @ # +! NumBq2 ");
AlKey. Add ("MagaZine @ (21 & * ID5 ");
AlKey. Add ("ThesisDSHI}. _ Y ");
String key = alKey [keyIndex]. ToString ();
DESCryptoServiceProvider provider = new DESCryptoServiceProvider ();
Provider. Key = Encoding. ASCII. GetBytes (key. Substring (0, 8 ));
Provider. IV = Encoding. ASCII. GetBytes (key. Substring (0, 8 ));
Byte [] buffer = new byte [str. Length/2];
For (int I = 0; I <(str. Length/2); I ++)
{
Int num2 = Convert. ToInt32 (str. Substring (I * 2, 2), 0x10 );
Buffer [I] = (byte) num2;
}
MemoryStream stream = new MemoryStream ();
CryptoStream stream2 = new CryptoStream (stream, provider. CreateDecryptor (), CryptoStreamMode. Write );
Stream2.Write (buffer, 0, buffer. Length );
Stream2.FlushFinalBlock ();
Stream. Close ();
Return Encoding. GetEncoding ("GB2312"). GetString (stream. ToArray ());
}
/// <Summary>
/// MD5 irreversible encryption 32-bit
/// </Summary>
/// <Param name = "s"> </param>
/// <Param name = "_ input_charset"> </param>
/// <Returns> </returns>
Public static string GetMD5_32 (string str1)
{
String cl1 = str1;
String pwd = "";
MD5 md5 = MD5.Create ();
// The encrypted data is a byte array.
Byte [] s = md5.ComputeHash (Encoding. Unicode. GetBytes (cl1 ));
// Convert an array of the byte type into a string by using a loop. This string is obtained by regular character formatting.
For (int I = 0; I <s. Length; I ++)
{
// Use the hexadecimal format of the obtained string. The characters in the format are lowercase letters. If uppercase letters (X) are used, the characters in the format are uppercase letters.
Pwd = pwd + s [I]. ToString ("x ");
}
Return pwd;
}
/// <Summary>
/// MD5 irreversible encryption 16-bit
/// </Summary>
/// <Param name = "ConvertString"> </param>
/// <Returns> </returns>
Public static string GetMd5_16 (string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider ();
String t2 = BitConverter. ToString (md5.ComputeHash (UTF8Encoding. Default. GetBytes (ConvertString), 4, 8 );
T2 = t2.Replace ("-","");
Return t2;
}
}
}