asp.net string Handling Class Code _ Practical Tips

Source: Internet
Author: User
Tags lowercase md5
Copy Code code 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 a 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>
Intercepts the string and restricts the string length more than the given length + ...
</summary>
<param name= "str" > the string to be intercepted </param>
<param name= "Len" > length of each line, more than this length of line wrap </param>
<param name= "Max" > Output string Maximum 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>
Intercepts a string, without limiting the length of the string
</summary>
<param name= "str" > the string to be intercepted </param>
<param name= "Len" > length of each line, more than this length of line wrap </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 the user's input is not malicious
</summary>
<param name= "Text" > Input string </param>
<param name= "MaxLength" > Max length </param>
<returns> the 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>
Uppercase characters in string to lowercase
</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)) >= && 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 a 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&AMP;*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 decryption GB2312
</summary>
<param name= "str" >desc string</param>
<param name= "Key" >key, must be 8-bit </param>
<returns></returns>
public static string Decode (string str, int keyIndex)
{
ArrayList Alkey = new ArrayList ();
Alkey.add ("bookt@#+! NumBq2 ");
Alkey.add ("magazine@" (21&AMP;*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 32-bit irreversible encryption
</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 ();
After encryption is an array of byte types
Byte[] s = Md5.computehash (Encoding.Unicode.GetBytes (CL1));
Converts an array of byte types to a string by using a loop, which is a regular character formatting gain
for (int i = 0; i < s.length; i++)
{
Use hexadecimal type format for the resulting string. The formatted character is a lowercase letter, and if uppercase (X) is used, the character in the format is upper case.
PWD = pwd + s[i]. ToString ("X");
}
return pwd;
}
<summary>
MD5 16-bit irreversible encryption
</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;
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.