Examples of string usages in C #

Source: Internet
Author: User

In C # programming, there is no more than a string, but sometimes due to the lack of careful or weak foundation and other factors prone to error, today this article will be a more detailed summary of the use of string in C #. Specific as follows:


1.string is a reference type, usually we compare the string object, compare the object's value instead of the object itself

As shown in the following code:

String stra= "ABCDE"; string strb= "abc"; string strc= "de"; Console.WriteLine (Stra = = (STRB+STRC));//trueconsole.writeline ((object) Stra = = (object) (STRB+STRC));//false

Because the string content is the same but the reference is not the same instance


2.string objects are non-modifiable

As shown in the following code:

String stra= "ABCDE"; stra= "AAAAA";

Seemingly modified stra content, in fact "ABCDE" has not been modified, but instead of a new object "AAAAA", and then assign the object's reference to Stra, and finally "ABCDE" will be garbage collected.


Creation of 3.string

Direct assignment:

String stra= "ABCDE";//Creates a string object with content ABCDE and assigns a reference to the object to the Stra

Structure:

Char[] arr={' A ', ' B ', ' C ', ' d ', ' E '};string stra=new string (arr);//Here are just a list of

Attention:

There is no string Str=new string ("ABCDE"); such constructs, String is the alias of string in the. NET Framework


4.string parameter passing

String is a reference type and we try to change that value in a function.

The test code is as follows:

static void Main (string[] args) {  string stra = "ABCDE";  Deal (stra);  Console.WriteLine (stra);  Console.ReadLine ();} static void Deal (String str) {  str = str. Substring (0, 2);}

Running Result: ABCDE

Reason is

When you pass a parameter of a reference type by value, it is possible to change the data that the reference points to, such as the value of a class member. However, you cannot change the value of the reference itself and pass the parameter through the REF keyword to resolve the problem

The revised code is as follows:

static void Main (string[] args) {   string stra = "ABCDE";   Deal (stra);   Console.WriteLine (ref stra);   Console.ReadLine ();} static void Deal (ref string str) {   str = str. Substring (0, 2);}

Results: AB


At this point the reference itself is passed, not the copy


5.null string and empty string

Null string: No memory allocated, empty string allocated memory, but no data in memory.

The test code is as follows:

static void Main (string[] args) {  string stra = "1";  String StrB = String. Empty;  string strc = null;  Console.WriteLine (int. Parse (Stra));//correct  Console.WriteLine (int). Parse (StrB));//The input string is malformed  Console.WriteLine (strc.tostring ());//The object reference is not set to an instance of the object.  console.readline ();}

Whether the built-in method string is null or empty:


IsNullOrEmpty equivalent to if (str = = NULL | | str. Equals (String.Empty))

Isnullorwhitespace equivalent to if (str = = NULL | | str. Equals (String.Empty) | | Str. Trim (). Equals (String.Empty))


6.StringBuilder

Now look at the following test code:

String stra= "abc" for (int i=0;i<10000;i++) {  stra+= "abc";} Consolse.writeline (Stra);

Although the code appears to use string concatenation to append new characters to an existing string named Stra, it actually creates a new string object for each concatenation operation. Greatly reduces the performance. You can use the StringBuilder class instead of the string class to change string values multiple times, StringBuilder objects are mutable, and when you append or delete substrings in a string, no new objects are created, but modifications are made on the original object. When you have finished modifying the value of a StringBuilder object, you can call its Stringbuilder.tostring method to convert it to a string

The modified test code is as follows:

StringBuilder stra=new StringBuilder (); for (int i=0;i<10000;i++) {stra.append ("abc");} Consolse.writeline (Stra.tostring ());

It is believed that the example mentioned in this paper can help and reference for us to grasp the string usage of C #.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Examples of string usages in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23633.html






Related content WinForm example of how to extract content using regular Expressions C # implement QQ-style feature instance code WPF popup with mask message box program code sample for C # read/write INI configuration file in Windows system share
Easy Learning C # hash table c#3.0 using EventLog class to write Windows event log methods C # using SendMessage to implement interprocess communication C # Multithreading Learning (ii) methods of manipulating a thread

Examples of string usages in C #

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.