asp.net string Operations base class (security, substitution, decomposition, etc.) _ Practical Tips

Source: Internet
Author: User
Tags chr httpcontext
/**********************************************************************************
*
* Function Description: Common function base class
* Author: Liu Gongxun;
* Version: V0.1 (c#2.0); Time: 2006-8-13
*
* *******************************************************************************/
/***************************************************************
* Update records
* 2007-1-5 Update:
* 1, take a few characters to 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>
Features: Replacing 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 Convert HTML operations on the contents of a table Form.
<summary>
Function: Transforms HTML operations on the contents of a table Form,
</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 Judge whether: return value: √orx
<summary>
Judge whether: Return value: √orx
</summary>
<param name= "B" >true or false</param>
<returns>√orx</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 >x</font></b>";
return s;
}
#endregion
#region Intercept string
<summary>
function: Intercept string length
</summary>
<param name= "str" > string to intercept </param>
<param name= "Length" > String length </param>
<param name= "FLG" >true: Add ..., flase: no </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 Intercept string + ...
<summary>
Intercept string + ...
</summary>
<param name= "Strinput" ></param>
<param name= "Intlen" ></param>
<returns></returns>
public static string Cutstring (string strinput, int intlen)/intercept 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 it's truncated, add half an ellipsis.
byte[] MyByte = System.Text.Encoding.Default.GetBytes (strinput);
if (MyByte. Length > Intlen)
{
strstring + = "...";
}
return strstring;
}
#endregion
#region String Division function
<summary>
String Division 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 exploded string as an array
<summary>
String Division function
</summary>
<param name= "str" > string to be decomposed </param>
<param name= "Splitstr" > Separator, 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 Get the Configuration field value in Web.config
<summary>
Get Global configuration parameters
</summary>
<param name= "key" > Key name </param>
<returns> Parameters </returns>
public static string Getapp (String key)
{
System.Configuration.AppSettingsReader appr = new System.Configuration.AppSettingsReader ();
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 passed-in string is yes/no
<summary>
Returns a bit based on whether the passed-in 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 detects whether a character character, exists in another character, exists, returns True, or returns false
<summary>
Detects a character character, whether it exists in another character, exists, returns True, or returns false
</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 detect if the string contains Chinese and Chinese length
<summary>
Detect if the string contains Chinese and Chinese length
</summary>
<param name= "str" > string to be detected </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)//Judge whether it is Chinese character or full foot symbol
{
l++;
}
}
return l;
}
#endregion
#region take a few characters to the right of the string
<summary>
Take a few characters to the right of the string
</summary>
<param name= "str" > String </param>
<param name= "Length" > several characters to 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 to the right </param>
<param name= "Straim" > the 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.