Summary of common functions of c#string

Source: Internet
Author: User

Add:

1: In C language char accounts for 1 bytes while in C#,java char accounts for two bytes in the database char in Han accounted for two byte alphanumeric for one word

2:string is a class in C #, String is the. NET Framework class string is a keyword, which means that string cannot be the name of a class, struct, enumeration, field, variable, method, property, and string can

A: string is Reference type

But when a parameter of type string is assigned to another parameter, only the value is given, not the address, because it remains on the heap, not on the stack

namespacestringop{classProgram {Static voidMain (string[] args) {            stringSTR1 ="Lipeng"; stringSTR2 =str1; Console.WriteLine ("str1="+str1); Console.WriteLine ("str2="+str2); STR1="Newlipeng"; Console.WriteLine ("str1="+str1); Console.WriteLine ("str2="+str2); }    }}
Two: Common function 1.all
// All function                // determines whether all elements in the sequence meet the criteria.             bool'b');            Console.WriteLine (issure);
2.CompareTo
// CompareTo               // returns a value that indicates whether the specified System.String object appears in this string.             // A 32-bit signed integer that indicates whether the instance precedes, follows, or appears in the same position in the sort order as the value parameter. Value condition less than 0 this instance is            located before str2//      . 0 The position of this instance in the sort order is the same as str2. Greater than 0 This instance is behind str2. -or-STR2 is null.             "newlipenaa";             INT LP = Str1.compareto (str2);            Console.WriteLine (LP);
3.Contains
// Contains              // returns a value that indicates whether the specified System.String object appears in this string.             bool iscontain = str1. Contains ("a");            Console.WriteLine (Iscontain);
4.Count
// Count             // returns a number that represents the number of elements in the specified sequence that satisfy a condition.             int'e'l');            Console.WriteLine (count);
5.IndexOf
// IndexOf              // reports the index of the first occurrence of a specified Unicode character in this string.             int index = str1. IndexOf ('a');            Console.WriteLine (index);             string STRs = str1. Substring (13);            Console.WriteLine (STRs);
6.Split
// Split               // The returned string array contains the substrings in this instance, separated by the elements of the specified Unicode character array.             string[] lsstr = str1. Split (newchar','e'  });             foreach (string in lsstr)            {                Console.WriteLine (a);            }
7.Trim and TrimEnd
// Trim                  // removes all leading and trailing white-space characters from the current System.String object.             // TrimEnd                  // removes all trailing occurrences of a set of characters specified in an array from the current System.String object.             string"   AAA  s";            Console.WriteLine (strspace);            Console.WriteLine (Strspace. Trim ());            Console.WriteLine (Strspace. TrimEnd ('s'));
8.PadLeft
// PadLeft              // returns a new string that, by padding the specified Unicode characters to the left of the characters in this instance, reaches the specified total length, so that the words are aligns aligned.             string padstr = str1. PadLeft ("a");            Console.WriteLine (PADSTR);
9.Replace
// Replace                  // returns a new string in which all specified Unicode characters appearing in this instance nonalphanumeric replaced by another specified Unicode character.             string replacestr = str1. Replace ('a''h');            Console.WriteLine (REPLACESTR);
10.Equals
// Equals                 // Determines whether this instance has the same value as another specified System.String object.             bool isequal = str1. Equals (REPLACESTR);            Console.WriteLine (isequal);
11.ToUpper
           //ToUpper              /// Returns the copy of this string converted to uppercase.             string upstr1 = str1. ToUpper ();           Console.WriteLine (UPSTR1);
Three: Variable string stringbuilder

StringBuilder objects are built to be more actionable:

It is best not to use string to change the content of the strings, because each generation of the object will have an impact on the system performance, especially when there is no reference object in memory, then the speed will be quite slow. If you use the StringBuffer class, the result will be different, and each result will operate on the StringBuilder object itself, instead of generating a new object and changing the object reference. Therefore, in general, it is recommended to use StringBuilder, especially if the string object changes frequently.

Simple Example :

StringBuilder Sber =NewStringBuilder (); //the most-added string ouput:lipeng,aaa,bbbSber. Append ("lipeng,aaa,bbb");            Console.WriteLine (Sber); //Remove Li ouput:peng,aaa,bbbSber. Remove (0,2);            Console.WriteLine (Sber); //Get length Ouput:12            intSberlength =Sber.            Length;            Console.WriteLine (Sberlength); //A string type method when converting to a string type             stringLPs = Sber. ToString (). Trim ();
IV: Other knowledge points

The count and length results of string types are the same as the number of char types in string and string arrays can only be recorded with one counter

Summary of common functions of c#string

Related Article

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.