Cstring knowledge point in. net

Source: Internet
Author: User

1. Calculate the length of two bytes (such as Chinese characters)

For example: String STR = '"I love China ";

If Str. length is used directly, the result 9 is found, where "China". Length = 2, which is a bit unconventional. It is especially troublesome to control the string output format.

 

Code
Private   String Getformatedstring ( String P_str, Int P_len)
{
// Method 1: calculate the number of Chinese Characters
// Int ihanzilen = encoding. Default. getbytecount (p_str)-p_str.length;
// Method 2: calculate the number of Chinese Characters
Int Ihanzilen = RegEx. Matches (p_str, @" [\ U4e00-\ u9fa5] " ). Count;
Ilen = P_len - Ilen;
String Sformat =   " {0 ,- "   + Ilen. tostring () +   " } " ;
Return   String . Format (sformat, p_str );
}

 

2. halfwidth and fullwidth Conversion

Code
Public   Static   String Charconverter ( String Source)
{
System. Text. stringbuilder result =   New System. Text. stringbuilder (source. length, source. Length );
For ( Int I =   0 ; I < Source. length; I ++ )
{
If (Source [I] > =   65281   && Source [I] <=   65373 )
{
Result. append (( Char ) (Source [I] -   65248 ));
}
Else   If (Source [I] =   12288 )
{
Result. append ( '   ' );
}
Else
{
Result. append (source [I]);
}
}

ReturnResult. tostring ();
}

 

3. Calculate the pixel information of a string in a specified font.

The calculation results are slightly different using the two methods.
Method 1:
First, reference graphic object G, as shown below:

Code
G. measurestring (STR,NewFont (fontfamily, fontsize, graphicsunit. pixel ));

Create a graphics object
Receives a reference to a graphic object in a form or control paint event as part of painteventargs. Create and draw a widgetCodeThis method is usually used to obtain references to graphic objects.
Call the creategraphics method of a control or form to obtain a reference to the graphics object. This object indicates the drawing of the control or form. Use this method to draw an existing form or control.
Create a graphics object from any object inherited from the image. This method is useful when you need to change an existing image.
Method 2:

Code
Textrenderer. measuretext (STR,NewFont (fontfamily, fontsize, graphicsunit. pixel ));

The system. Windows. Forms namespace must be referenced above.

 

4. Obtain the numbers in the string.

Code
Public   Decimal Getnumber ( String Str)
{
Decimal Result =   0 ;
If (Str ! =   Null   && Str ! =   String . Empty)
{
// Regular Expressions remove non-numeric characters (excluding decimal points .)
Str = RegEx. Replace (STR, @" [^ \ D. \ D] " , "" );

//If it is a number, convert it to decimal type
If(RegEx. ismatch (STR,@"^ [+-]? \ D * [.]? \ D * $"))
{
Result= Decimal. Parse (STR );
}
}

ReturnResult;
}

 

4. Rounding of the Double Type

We often encounter rounding issues. The following two methods are provided:

1. Use the round Method

Using system. Math;
......
Round (3.045, 2) // return value: 3.05
Round (3.044, 2) // return value: 3.04

Ii. String formatting

Double Pi = 3.1415936;
......
String strpi = pi. tostring ("F3"); // return value: 3.142

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.