Set of processing functions for strings

Source: Internet
Author: User
The following string has a set of processing functions from Seaskyer
#region "set of processing functions for strings"

<summary>
Set of processing functions for strings
</summary>
public class Str
{
<summary>
Converts a Stream into a string
</summary>
<param name= "s" > Stream flow </param>
<returns> string </returns>
public static string convertstreamtostring (Stream s)
{
String strresult = "";
StreamReader sr = new StreamReader (s, Encoding.UTF8);

char[] Read = new char[256];

Read 256 charcters at a time.
int count = Sr. Read (read, 0, 256);

while (Count > 0)
{
Dump the 256 characters on a string and display the string onto the console.
String str = new string (read, 0, Count);
Strresult + + str;
Count = Sr. Read (read, 0, 256);
}


Releasing resources
Sr. Close ();

return strresult;
}

<summary>
Handles the passed parameter string to prevent an injection attack
</summary>
<param name= "str" > passed parameter string </param>
<returns> String </returns>
public static string Convertsql (String str)
{
str = str. Trim ();
str = str. Replace ("'", "" ");
str = str. Replace (";-", "");
str = str. Replace ("=", "");
str = str. Replace ("or", "");
str = str. Replace ("and", "");

return str;
}


<summary>
Format the output of the occupied space size
</summary>
<param name= "Size" > Size </param>
<returns> return String </returns>
public static string Formatnum (long size)
{
Decimal NUM;
String strresult;

if (Size > 1073741824)
{
NUM = (convert.todecimal (size)/Convert.todecimal (1073741824));
strresult = NUM. ToString ("N") + "M";
}
else if (Size > 1048576)
{
NUM = (convert.todecimal (size)/Convert.todecimal (1048576));
strresult = NUM. ToString ("N") + "M";
}
else if (Size > 1024)
{
NUM = (convert.todecimal (size)/convert.todecimal (1024));
strresult = NUM. ToString ("N") + "KB";
}
Else
{
strresult = size + "byte";
}

return strresult;
}

<summary>
To determine whether a string is a valid mailing address
</summary>
<param name= "Email" ></param>
<returns></returns>
public static bool IsValidEmail (string email)
{
return Regex.IsMatch (email, @ "^.+@") [a-za-z0-9-.] +. ([a-za-z]{2,3}| [0-9] {1,3}) (]?) $ " );
}

<summary>
Determines whether a string is a valid URL address
</summary>
<param name= "url" ></param>
<returns></returns>
public static bool Isvalidurl (string URL)
{
return Regex.IsMatch (URL, @ "^ (http|https|ftp)://[a-za-z0-9-.] +. [A-za-z] {2,3} (: [a-za-z0-9]*)?/? ([A-za-z0-9-._, '//+&%$#=~]) *[^.,) (s]$ ");
}

<summary>
that determines whether the string is of type int
</summary>
<param name= "Val" ></param>
<returns></returns>
public static bool Isvalidint (string val)
{
Return Regex.IsMatch (val, @ "^[1-9]d*.") [0]*$ ");
}

<summary>
Detects whether a string is all positive integers
</summary>
<param name= "str" ></param>
<returns></returns>
public static bool Isnum (String str)
{
BOOL B
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.