Asp.net string operation base class (security, replacement, decomposition, etc)

Source: Internet
Author: User

/*************************************** **************************************** ***
*
* Function Description: common function base classes
* Author: Liu gongxun;
* Version: V0.1 (C #2.0); time:
*
**************************************** ****************************************/
/*************************************** ************************
* Update records
* Update:
* 1. Take the characters on the right of the string.
* 2. Replace the string on the right.
**************************************** ************************/
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Text;
Namespace EC
{
/// <Summary>
/// Common function base class
/// </Summary>
Public class FunObject
{
# Region replacement string
/// <Summary>
/// Function: replace characters
/// </Summary>
/// <Param name = "strVAlue"> string </param>
/// <Returns> replace 'string </returns>
Public static string FilterSQL (string strVAlue)
{
String str = "";
Str = strVAlue. Replace ("''","");
Return str;
}
# Endregion
# Region converts the table content to HTML,
/// <Summary>
/// Function: Convert the table content to HTML,
/// </Summary>
/// <Param name = "fString"> html string </param>
/// <Returns> </returns>
Public static string HtmlCode (string fString)
{
String str = "";
Str = fString. Replace (">", "> ");
Str = fString. Replace ("<", "<");
Str = fString. Replace ("","");
Str = fString. Replace ("\ n", "<br/> ");
Str = fString. Replace ("\ r", "<br/> ");
Str = fString. Replace ("\ r \ n", "<br/> ");
Return str;
}
# Endregion
# Region: returns √ or ×
/// <Summary>
/// Determine whether: Return Value: √ or ×
/// </Summary>
/// <Param name = "B"> true or false </param>
/// <Returns> √ or × </returns>
Public static string Judgement (bool B)
{
String s = "";
If (B = true)
S = "<B> <font color = #009900> √ </font> </B> ";
Else
S = "<B> <font color = # FF0000> × </font> </B> ";
Return s;
}
# Endregion
# Region truncated string
/// <Summary>
/// Function: truncates the string length.
/// </Summary>
/// <Param name = "str"> string to be intercepted </param>
/// <Param name = "length"> String length </param>
/// <Param name = "flg"> true: add..., flase: do not add </param>
/// <Returns> </returns>
Public static string GetString (string str, int length, bool flg)
{
Int I = 0, j = 0;
Foreach (char chr in str)
{
If (int) chr> 127)
{
I + = 2;
}
Else
{
I ++;
}
If (I> length)
{
Str = str. Substring (0, j );
If (flg)
Str + = "......";
Break;
}
J ++;
}
Return str;
}
# Endregion
# Region truncation string +...
/// <Summary>
/// Capture string +...
/// </Summary>
/// <Param name = "strInput"> </param>
/// <Param name = "intlen"> </param>
/// <Returns> </returns>
Public static string CutString (string strInput, int intlen) // truncates a string
{
ASCIIEncoding ascii = new ASCIIEncoding ();
Int intLength = 0;
String strString = "";
Byte [] s = ascii. GetBytes (strInput );
For (int I = 0; I <s. Length; I ++)
{
If (int) s [I] = 63)
{
IntLength + = 2;
}
Else
{
IntLength + = 1;
}
Try
{
StrString + = strInput. Substring (I, 1 );
}
Catch
{
Break;
}
If (intLength> intlen)
{
Break;
}
}
// If yes, add half ellipsis
Byte [] mybyte = System. Text. Encoding. Default. GetBytes (strInput );
If (mybyte. Length> intlen)
{
StrString + = "... ";
}
Return strString;
}
# Endregion
# Region string Score Function
/// <Summary>
/// String Function
/// </Summary>
/// <Param name = "strID"> </param>
/// <Param name = "index"> </param>
/// <Param name = "Separ"> </param>
/// <Returns> </returns>
Public string StringSplit (string strings, int index, string Separ)
{
String [] s = strings. Split (char. Parse (Separ ));
Return s [index];
}
# Endregion
# Region decomposition string is an array
/// <Summary>
/// String Function
/// </Summary>
/// <Param name = "str"> string to be decomposed </param>
/// <Param name = "splitstr"> delimiter, which can be string type </param>
/// <Returns> character array </returns>
Public static string [] splitstr (string str, string splitstr)
{
If (splitstr! = "")
{
System. Collections. ArrayList c = new System. Collections. ArrayList ();
While (true)
{
Int thissplitindex = str. IndexOf (splitstr );
If (thissplitindex> = 0)
{
C. Add (str. Substring (0, thissplitindex ));
Str = str. Substring (thissplitindex + splitstr. Length );
}
Else
{
C. Add (str );
Break;
}
}
String [] d = new string [c. Count];
For (int I = 0; I <c. Count; I ++)
{
D [I] = c [I]. ToString ();
}
Return d;
}
Else
{
Return new string [] {str };
}
}
# Endregion
# Region URL Encoding
/// <Summary>
/// URL Encoding
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> </returns>
Public static string UrlEncoding (string str)
{
Byte [] bytes = System. Text. Encoding. UTF8.GetBytes (str );
Return System. Text. Encoding. UTF8.GetString (bytes). ToString ();
}
# Endregion
# Region obtain the configuration field value in Web. config
/// <Summary>
/// Obtain global configuration parameters
/// </Summary>
/// <Param name = "key"> key name </param>
/// <Returns> parameter </returns>
Public static string GetApp (string key)
{
System. Configuration. AppSettingsReader appr = new System. Configuration. etettingsreader ();
Try
{
String str = (string) appr. GetValue (key, typeof (string ));
If (str = null | str = "") return null;
Return str;
}
Catch (Exception E ){}
Return null;
}
# Endregion
# Region returns a Bit based on whether the input string is yes/no
/// <Summary>
/// Bit is returned based on whether the input string is yes/no
/// </Summary>
/// <Param name = "flg"> </param>
/// <Returns> </returns>
Public static int GetBitBool (string flg)
{
Int str = 0;
Switch (flg. ToLower ())
{
Case "yes ":
Str = 1;
Break;
Case "no ":
Str = 0;
Break;
Default:
Break;
}
Return str;
}
# Endregion
# Region Html Encoding
/// <Summary>
/// HTML Encoding
/// </Summary>
/// <Param name = "strInput"> </param>
/// <Returns> </returns>
Public static string HtmlEncode (string strInput)
{
String str;
Try
{
Str = HttpContext. Current. Server. HtmlEncode (strInput );
}
Catch
{
Str = "error ";
}
Return str;
}
/// <Summary>
/// HTML Decoding
/// </Summary>
/// <Param name = "strInput"> </param>
/// <Returns> </returns>
Public static string HtmlDecode (string strInput)
{
String str;
Try
{
Str = HttpContext. Current. Server. HtmlDecode (strInput );
}
Catch
{
Str = "error ";
}
Return str;
}
# Endregion
# Region checks whether a character is included in another character. If yes, true is returned. Otherwise, false is returned.
/// <Summary>
/// Check whether a character is in another character. If yes, true is returned. Otherwise, false is returned.
/// </Summary>
/// <Param name = "srcString"> original string </param>
/// <Param name = "aimString"> Target string </param>
/// <Returns> </returns>
Public static bool IsEnglish (string srcString, string aimString)
{
Bool Rev = true;
String chr;
If (aimString = "" | aimString = null) return false;
For (int I = 0; I <aimString. Length; I ++)
{
Chr = aimString. Substring (I, 1 );
If (srcString. IndexOf (chr) <0)
{
Return false;
Break;
}
}
Return Rev;
}
# Endregion
# Region check whether the string contains Chinese characters and Chinese characters
/// <Summary>
/// Check whether the string contains Chinese characters and Chinese characters
/// </Summary>
/// <Param name = "str"> string to be checked </param>
/// <Returns> Chinese String Length </returns>
Public static int CnStringLength (string str)
{
ASCIIEncoding n = new ASCIIEncoding ();
Byte [] B = n. GetBytes (str );
Int l = 0; // l is the actual length of the string
For (int I = 0; I <= B. Length-1; I ++)
{
If (B [I] = 63) // determines whether it is a Chinese character or full-legged symbol.
{
L ++;
}
}
Return l;
}
# Endregion
# Region: several characters on the right of the string
/// <Summary>
/// Take the characters on the right of the string
/// </Summary>
/// <Param name = "str"> string </param>
/// <Param name = "length"> several characters on the right </param>
/// <Returns> </returns>
Public static string GetStrRight (string str, int length)
{
String Rev = "";
If (str. Length <length)
{
Rev = str;
}
Else
{
Rev = str. Substring (str. Length-length, length );
}
Return Rev;
}
# Endregion
# Region Replace the string on the right
/// <Summary>
/// Replace the string on the right
/// </Summary>
/// <Param name = "str"> string </param>
/// <Param name = "strsrc"> string on the right </param>
/// <Param name = "straim"> string to be replaced </param>
/// <Returns> </returns>
Public static string RepStrRight (string str, string strsrc, string straim)
{
String Rev = "";
If (GetStrRight (str, strsrc. Length )! = Strsrc)
{
Rev = str;
}
Else
{
Rev = str. Substring (0, str. Length-strsrc. Length). ToString () + straim. ToString ();
}
Return Rev;
}
# Endregion
}
}
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.