Paste several functions deducted from the DNT Forum code

Source: Internet
Author: User

He has been reading what he wrote over the past few days.Code. Some codes are useful and can be shared with you.

The code below is all found from the utils. CS class.

/// <Summary>
/// Returns the true length of the string. The length of one Chinese character is 2.
/// </Summary>
/// <Returns> character length </returns>
Public static int getstringlength (string Str)
{
Return encoding. Default. getbytes (STR). length;
}

 

/// <Summary>
/// Determine the position of the specified string in the specified String Array
/// </Summary>
/// <Param name = "strsearch"> string </param>
/// <Param name = "stringarray"> String Array </param>
/// <Param name = "caseinsensetive"> case insensitive; true indicates case insensitive; false indicates case insensitive. </param>
/// <Returns> the position of the string in the specified string array. If the string does not exist,-1 is returned. </returns>
Public static int getinarrayid (string strsearch, string [] stringarray, bool caseinsensetive)
{
For (INT I = 0; I <stringarray. length; I ++)
{
If (caseinsensetive)
{
If (strsearch. tolower () = stringarray [I]. tolower ())
Return I;
}
Else if (strsearch = stringarray [I])
Return I;
}
Return-1;
}

 

/// <Summary>
/// MD5 Function
/// </Summary>
/// <Param name = "str"> original string </param>
/// <Returns> MD5 result </returns>
Public static string MD5 (string Str)
{
Byte [] B = encoding. Default. getbytes (STR );
B = new md5cryptoserviceprovider (). computehash (B );
String ret = "";
For (INT I = 0; I <B. length; I ++)
RET + = B [I]. tostring ("X"). padleft (2, '0 ');

Return ret;
}

 

/// <Summary>
/// Sha256 Function
/// </Summary>
///// <Param name = "str"> original string </param>
/// <Returns> sha256 result </returns>
Public static string sha256 (string Str)
{
Byte [] sha256data = encoding. utf8.getbytes (STR );
Sha256managed sha256 = new sha256managed ();
Byte [] result = sha256.computehash (sha256data );
Return convert. tobase64string (result); // return a string of 44 bytes.
}

 

/// <Summary>
/// Return the name of the month based on Arabic numerals (which can be changed to a certain language)
/// </Summary>
Public static string [] monthes
{
Get
{
Return New String [] {"January", "February", "March", "0000l", "may", "June", "July", "August ", "September", "October", "November", "December "};
}
}

 

///


// clear repeated items in the string array
///
// String Array
// maximum length of a single element in a string array
///
Public static string [] distinctstringarray (string [] strarray, int maxelementlength)
{< br> hashtable H = new hashtable ();

foreach (string s in strarray)
{< br> string K = s;
If (maxelementlength> 0 & K. length> maxelementlength)
{< br> K = K. substring (0, maxelementlength);
}< br> H [K. trim ()] = s;
}

String [] result = new string [H. Count];

H. Keys. copyto (result, 0 );

Return result;
}

/// <Summary>
/// Format the number of bytes string
/// </Summary>
/// <Param name = "bytes"> </param>
/// <Returns> </returns>
Public static string formatbytesstr (INT bytes)
{
If (Bytes> 1073741824)
Return (double) (Bytes/1073741824). tostring ("0") + "G ";

If (Bytes> 1048576)
Return (double) (Bytes/1048576). tostring ("0") + "M ";

If (Bytes> 1024)
Return (double) (Bytes/1024). tostring ("0") + "K ";

Return bytes. tostring () + "bytes ";
}

/// <Summary>
/// Whether it is an IP address
/// </Summary>
/// <Param name = "ip"> </param>
/// <Returns> </returns>
Public static bool isip (string IP)
{
Return RegEx. ismatch (IP, @ "^ (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) \.) {3} (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) $ ");
}

 

/// <Summary>
/// Convert a fullwidth number to a number
/// </Summary>
/// <Param name = "sbccase"> </param>
/// <Returns> </returns>
Public static string sbccasetonumberic (string sbccase)
{
Char [] C = sbccase. tochararray ();
For (INT I = 0; I <C. length; I ++)
{
Byte [] B = system. Text. encoding. Unicode. getbytes (C, I, 1 );
If (B. Length = 2)
{
If (B [1] = 255)
{
B [0] = (byte) (B [0] + 32 );
B [1] = 0;
C [I] = system. Text. encoding. Unicode. getchars (B) [0];
}
}
}
Return new string (C );
}

 

/// <Summary>
/// Convert the string to color
/// </Summary>
/// <Param name = "color"> </param>
/// <Returns> </returns>
Public static color tocolor (string color)
{
Int red, green, blue = 0;
Char [] RGB;
Color = color. trimstart ('#');
Color = RegEx. Replace (color. tolower (), "[G-ZG-Z]", "");
Switch (color. length)
{
Case 3:
RGB = color. tochararray ();
Red = convert. toint32 (RGB [0]. tostring () + RGB [0]. tostring (), 16 );
Green = convert. toint32 (RGB [1]. tostring () + RGB [1]. tostring (), 16 );
Blue = convert. toint32 (RGB [2]. tostring () + RGB [2]. tostring (), 16 );
Return color. fromargb (red, green, blue );
Case 6:
RGB = color. tochararray ();
Red = convert. toint32 (RGB [0]. tostring () + RGB [1]. tostring (), 16 );
Green = convert. toint32 (RGB [2]. tostring () + RGB [3]. tostring (), 16 );
Blue = convert. toint32 (RGB [4]. tostring () + RGB [5]. tostring (), 16 );
Return color. fromargb (red, green, blue );
Default:
Return color. fromname (color );

}
}

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.