#. C #--strings (string)

Source: Internet
Author: User

Theory:

C # string (string)


In C #, you can use character arrays to represent strings, but it is more common to use the string keyword to declare a string variable.


The string keyword is an alias for the System.String class.


Create a String object you can use one of the following methods to create a string object:

By specifying a string for the string variable

By using the String class constructor

By using the string concatenation operator (+)

By retrieving a property or calling a return character:


Properties of the String class

The String class has the following two properties:

1. Chars Gets the specified position of the Char object in the current String object.

2. Length Gets the number of characters in the current String object.


Methods of the String class

The string class has many methods for manipulating the string object. The following table provides some of the most common methods:

650) this.width=650; "title=" Capture.png "src=" http://s3.51cto.com/wyfs02/M02/6D/D8/ Wkiom1vti7dxx4t4aasycto9pym742.jpg "alt=" Wkiom1vti7dxx4t4aasycto9pym742.jpg "/>

650) this.width=650; "title=" Capture.2png.png "src=" http://s3.51cto.com/wyfs02/M01/6D/D8/ Wkiom1vti53wycnjaawpnfh0m4m059.jpg "alt=" Wkiom1vti53wycnjaawpnfh0m4m059.jpg "/>

650) this.width=650; "title=" Capture3.png "src=" http://s3.51cto.com/wyfs02/M00/6D/D8/ Wkiom1vti8syh9c-aahiynx8sfw886.jpg "alt=" Wkiom1vti8syh9c-aahiynx8sfw886.jpg "/>



Example 1:

using system;namespace stringapplication{    class program     {        static void main (String[] args)         {            //string, string connection             string fname,  lname;            fname =  "Rowan";             lname =  "Atkinson";             string fullname = fname  + lname;            console.writeline ("full  name: {0} ",  fullname);             By using the  string  structure letterNumber             char[] letters = {   ' H ',  ' e ',  ' l ',  ' l ', ' O '  };             string greetings = new string (Letters);             console.writeline ("greetings: {0}",  greetings);             //method Return String              string[] sarray = {  "Hello",  "from",  " Tutorials ", " point " };            string  message = string.join (" ",  sarray);             console.writeline ("message: {0}",  message);             //formatting methods for conversion values             DateTime  Waiting = new datetime (2012, 10, 10, 17, 58, 1);             string chat = string.format ("Message  SENT AT {0:T} ON {0:D} ",              waiting);             Console.WriteLine ("Message: {0}",  chat);             console.readkey ()  ;        }    } }//when the above code is compiled and executed, it produces the following results://full name: rowan atkinson//greetings: hello//message:  Hello from tutorials point//message: message sent at 5:58 pm on  wednesday, october 10, 2012 


Example 2:

The following example shows some of the methods mentioned above:

Comparing strings

using system;namespace stringapplication{   class stringprog   {       static void main (String[] args)        {         string str1 =  "This is  test ";         string str2 = " This is  text ";         if  (String.Compare (STR1, STR2)  == 0)          {             console.writeline (str1 +  " and "  + str2  +   " are equal.");          }          Else         {      &nBsp;     console.writeline (str1 +  " and "  + str2 +   " are not equal.");          }          Console.readkey ()  ;      }   } When the above code is compiled and executed, it produces the following result: this  is test and this is text are not equal.


String containing string:

Using System;namespace stringapplication{class Stringprog {static void Main (string[] args) {Strin         G str = "This is test"; if (str.         Contains ("test")) {Console.WriteLine ("The Sequence ' test ' was found.");      } console.readkey (); }   }}


When the above code is compiled and executed, it produces the following results:

the sequence ' Test ' was found .


Get substring:

Using System;namespace stringapplication{class Stringprog {static void Main (string[] args) {Strin         G str = "Last night I dreamt of San Pedro";         Console.WriteLine (str); string substr = str.         Substring (23);      Console.WriteLine (SUBSTR);   } console.readkey (); }}//when the above code is compiled and executed, it produces the following results: San Pedro

Connection string:

using system;namespace stringapplication{   class stringprog   {       static void main (String[] args)        {         string[] starray = new  string[]{"Down the way nights are dark",           "And the sun shines daily on the mountain top",           "i took a trip on a sailing  ship ",         " and when i reached  Jamaica ",         " I made a stop "};          string str = string.join ("\ n",  starray);        &Nbsp; console.writeline (str);      }       Console.readkey ()  ;   }}

When the above code is compiled and executed, it produces the following results:

Down the the-the-nights is Darkand the sun shines daily on the mountain Topi took a-trip to a sailing shipand when I reached Jamaicai made a stop


Reference:
Http://outofmemory.cn/csharp/tutorial/csharp-string.html



This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1657426

#. C #--strings (string)

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.