C # optimizing string operations)

Source: Internet
Author: User

1. Convert. toint32 and int32.parse
2. Three Methods of split
3. What is the difference between @ "ABC" and "ABC "?
4. retain two valid decimal places and rounding them
5. url-based Chinese delivery solution
6. Convert 123456789 to 12-345-67893Method
7. Swap two characters at the specified position4Method
8. Wonderful use of "% 10"
9. Output 21 aaaaaaaaaaaaaaaaaaaaaaa clever practices

 

1.Convert. toint32AndInt32.parse

The two methods can resolve string to int, so we will have doubts: What is the difference between them? When should I use it? Performance.

In fact, int32.tryparse in 2.0 also achieves the same effect.

Execute three methods1 million ConversionsViewMillisecondOfDifferences:

First time Second Third time
Convert. toint32 () 532-204 = 328 1163-750 = 413 782-469 = 313
Int32.parse () 844-532 = 312 360-63 = 297 1094-782 = 312
Int32.tryparse () 1141-844 = 297 657-360 = 297 375-94 = 281

 

In fact, we can draw a conclusion:
There are almost no differences between the three methods!
If you really want to pursue perfection, the performance difference is: int32.tryparse () is better than int32.parse () than convert. toint32 ().
Therefore, we recommend that you use int32.parse () in. net1.1 and int32.tryparse () in. net2.0 ().

So why?
In fact, this data is not accidental, because:
Convert. toint32 will delegate the final parsing work to int32.parse;
Int32.parse will delegate the final parsing work to number. parseint32;
Int32.tryparse will delegate the final parsing work to number. tryparseint32

2. Three Methods of split
We can spell 12 33 456 12342 as a single character, because it is very troublesome to use arrays when dealing with only a few groups, so we use "|" or ", "and so on. //

String AA = "1234,234523, 4324,324 ";
String [] cc = AA. Split (New char []... {','});
--------------------------------------------------
String [] STR = AA. Split (',');
--------------------------------------------------
String A = "1, 2, 3, 4, 5, 6, 7, 8, 9 ";
String B = ",";
String [] C = Split (A, B );
--------------------------------------------------
String [] arr = system. Text. regularexpressions. RegEx. Split (input, pattern );

3. What is the difference between @ "ABC" and "ABC "?

@ "ABC" is no different from "ABC"

 

4. retain two valid decimal places and rounding them
This is another common problem.
Retain two valid decimal places (the same parameter as retaining n digits ):
Double A = 12.345678;
Console. Write (math. Round (A, 2 ));

Double A = 12.345678; // It is the number rounded up.
Console. Write (a x 10000 + 0.5)/10000 );

 

5. url-based Chinese delivery solution

1. Set the Web. config file.
<System. Web>
......
<Globalization requestencoding = "gb2312" responseencoding = "gb2312" Culture = "ZH-CN" fileencoding = "gb2312"/>
......
</System. Web>
2. Before passing Chinese characters, encode the Chinese parameters to be passed and decode them upon receiving them.
> Transfer
String name = "Chinese parameter ";
Response. Redirect ("B. aspx? Name = "+ server. urlencode (name ));
> Receive
String name = request. querystring ["name"];
Response. Write (server. urldecode (name ));

3. If a Chinese parameter is transferred from the. html file to the. aspx file (that is, URL conversion is not performed using the Redirect () method from the background ). Similarly, the passed Chinese parameters must be encoded and decoded upon receiving.
> Transfer
<Script language = "JavaScript">
Function gourl ()
{
VaR name = "Chinese parameter ";
Location. href = "B. aspx? Name = "+ escape (name );
}
</SCRIPT>
<Body onclick = "gourl ()">
> Receive
String name = request. querystring ["name"];
Response. Write (server. urldecode (name ));

In general. Set the Web. config file. However, if you use JavaScript to call the WebService method (passing Chinese parameters to WebService ). The Web. config file is invalid.

6. Convert 123456789 to 12-345-6789.
String A = "123456789 ";
A = int. parse (a). tostring ("##-###-####");
Console. Write ();
--------------------------------------------------
String A = "123456789 ";
A = A. insert (5, "-"). insert (2 ,"-");
Console. Write ();
--------------------------------------------------
String A = "123456789 ";
RegEx Reg = new RegEx (@ "^ (d) $ ");
A = reg. Replace (,"--");
Console. Write ();

 

7. Four Methods for exchanging characters at specified positions
{
String S = "123456789 ";
Swapchar (ref S, 3, 6 );
Console. Write (S. tostring ());
}

Static void swapchar (ref string S, int I1, int I2)
{
Char temp = s [I1];
Char temp1 = s [I2];
S = S. Remove (I1, 1). insert (I1, temp1.tostring ());
S = S. Remove (I2, 1). insert (I2, temp. tostring ());
}
---------------------------------------------------------
{
String S = "123456789 ";
Console. Write (swapchar (s, 3, 6). tostring ());
}
Static string swapchar (string S, int P1, int P2)
{
If (p1 = P2) | (P1 <0) | (P2 <0) return S;
If (P1> = S. Length) | (P2> = S. Length) return S;
Char [] vchars = S. tochararray ();
Vchars [P1] = (char) (vchars [P2] | (vchars [P2] = vchars [P1]) & 0 );
Return new string (vchars );
}
--------------------------------------------------------------
Static void main ()
{
String S = "123456789 ";
Console. Write (swapchar (s, 3, 6). tostring ());
}

Public static string swapchar (string STR, int A, int B)
{
Char [] newstr = Str. tochararray ();
Newstr [a] = STR [B];
Newstr [B] = STR [a];
Return new string (newstr );
}
--------------------------------------------------------------
Static void main ()
{
String S = "123456789 ";
Console. Write (swapchar (s, 3, 6). tostring ());
}

Static string swapchar (string S, int P1, int P2)
{
If (P1> P2)... {int P = p1; P1 = P2; P2 = P ;}
Return RegEx. replace (S, "^ (. {"+ p1 + "})(.) (. {"+ (P2-P1-1) + "})(.) (. *) $ ","");
}

 

8. Wonderful use of "% 10"

Random r = new random ();

Console. writeline (R. Next () % 10); // 1-9
Console. writeline (R. Next () % 10 );
Console. writeline (R. Next () % 10 );
Console. writeline (R. Next () % 10 );
Console. writeline (R. Next () % 10 );
Console. writeline (R. Next () % 10 );
Console. writeline (R. Next () % 10 );
Console. writeline (R. Next () % 10 );
Console. writeline (R. Next () % 10 );

 

9. Output 21 aaaaaaaaaaaaaaaaaaaaaaa clever practices
Understanding of the new Constructor
What do you do if you want to create a string consisting of 21 "A" characters?
String STR = "aaaaaaaaaaaaaaaaaaaaa"; // honest
String STR = new string ('A', 21); // simple and intelligent

10. Compare () and compareto () Methods
The same function is just two methods of two interfaces.
Compare is the comparatable method, and compareto is the comparator method.

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.