C # preprocessor directives and string handling

Source: Internet
Author: User

One, C # preprocessor directives

In C # programming, if the code in the file is too long, it is not easy to read, so the C # preprocessor directive #region is used to process the code. #region is the C # preprocessor directive. #region is a block preprocessing command, which is primarily a block for editor code and is automatically deleted at compile time.

#region enables programmers to specify which code can be expanded or collapsed when using the outlining feature of the Visual Studio Code Editor. Using #region and #endregion, write the code between the two.

public void Insert (string insertstr)

{

Mysqlconnection conn = new mysqlconnection ();

Conn. ConnectionString = Mysqlconstr;

Mysqlcommand comm = new Mysqlcommand (INSERTSTR, conn);

OpenConnection (conn);

#endregion

After clicking #region, you can see the above code, which makes it easy to read the code

Second, C # string processing-string and StringBuilder

Let's start with the String class, which is the character processing class, which provides a number of ways to handle strings.

Usage: string sqlconnstr= "Bianceng"; If you try to use new here, there will be an error, because wait a minute. The method provided by the string class looks at the program in the following table and can be used to determine the effect of the method against the result on the right.

Console.WriteLine ("Method of String Class");

String StrText = "larger than me";

Console.WriteLine (Strtext.clone ());//clone, return reference to instance

Console.WriteLine (Strtext.compareto ("Larger than Me"))//Compare two objects

Console.WriteLine (Strtext.contains ("than"))//To determine if the object contains "than" and return true

Console.WriteLine (Strtext.endswith ("Me"));//To determine if the end of the object matches "Me", is to return true

Console.WriteLine (Strtext.equals ("larger"))//Judgment object has the same value as the Parameter object

Console.WriteLine (Strtext.getenumerator ());

Console.WriteLine (Strtext.gethashcode ());//hash code

Console.WriteLine (Strtext.gettype ());//Type

Console.WriteLine (Strtext.indexof ("L"))//The position of the parameter L in the object

Console.WriteLine (Strtext.insert (1, "you"))//insert "you" in place

Console.WriteLine (Strtext.isnormalized ()),//whether the object conforms to the Unicode paradigm

Console.WriteLine (Strtext.lastindexof (' m '));//parameter m last seen position

Console.WriteLine (strtext.length);//Length of Object

Console.WriteLine (Strtext.normalize ());

Console.WriteLine (strtext.padleft, ' y '));///Fill the object with Y left to satisfy its length

Console.WriteLine (Strtext.padright, ' X '));///Fill the object with an X right to satisfy the length

Console.WriteLine (Strtext.remove (5));/move out all characters after starting at the specified position

Console.WriteLine (Strtext.replace (' A ', ' Y '));//Replace

Console.WriteLine (Strtext.split (' y '));

Console.WriteLine (Strtext.startswith ("Lar"))//judge whether to start with "Lar"

Console.WriteLine (Strtext.substring (2,6));//substring from start length

Console.WriteLine (Strtext.tochararray ());//Convert an object to an array of characters

Console.WriteLine (Strtext.tolower ());//convert to lowercase copy

Console.WriteLine (Strtext.tolowerinvariant ());

Console.WriteLine (Strtext.tostring ());

Console.WriteLine (Strtext.toupper ());//Capital Form

Console.WriteLine (Strtext.toupperinvariant ());

Console.WriteLine (Strtext.trim ()); The empty object in the object

Console.WriteLine (Strtext.trimend (' m ', ' e '));//Move out of ' m ', ' e ' after the object

Some methods are too simple to introduce, of course, to the reader's own programming to understand, master various methods.

Third, the use of the Stringbuiler class method of processing strings.

The StringBuilder class is a dynamic string that can be deleted and incremented after StringBuilder object creation, and is performed on the same object, MyStr. Append ("new") does not create a new object after execution, and its results are saved in the original place. This is not the same as the string class. The construction methods of the StringBuilder class are:

StringBuilder obj=new StringBuilder ("Biancdheng"); The default size is 16 bytes; StringBuilder obj2=new StringBuilder ("Biancheng", 18); /specified as 18 bytes. Look at the StringBuilder offer method:

StringBuilder mystr =new StringBuilder ("Larger than Me");

Console.WriteLine (mystr. Append ("new"); add "new" after//mystr

Console.WriteLine (mystr. Appendline ("Loveme"));

Appends the default line terminator to the end of the current StringBuilder object

Console.WriteLine (mystr. Capacity);//object's maximum number of characters

Console.WriteLine (mystr. Ensurecapacity (20));//instance object ensure that at least a value is specified

Console.WriteLine (mystr. Equals (New StringBuilder ("larger"));//Determine whether two objects are equal

Console.WriteLine (mystr. GetHashCode ());//Get hash code

Console.WriteLine (mystr. GetType ());//Get the type of the object

Console.WriteLine (mystr. Insert (0, "Insert")); Inserts a small character at the specified location

Console.WriteLine (mystr. length);//Get the lengths of the instance objects

Console.WriteLine (mystr. maxcapacity);//The maximum capacity of the instance object

Console.WriteLine (mystr. Remove (1, 2)),//remove (startindex,length) position and length two parameters removed

Console.WriteLine (mystr. Replace (' e ', ' Y '));//Substitute the second parameter for the first argument

Console.WriteLine (mystr. ToString ());//Convert to character

The results of the above program run are:

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.