String and array

Source: Internet
Author: User
Tags shallow copy

String:

The most frequently used type in an application is a string. String short strings, is a special linear table, its particularity is that the data element in the string is a character. Strings are widely used in many aspects of the computer. The source and target programs are string data, as in compilation and high-level language compilation programs. In the transaction process, the customer's information, such as name, address, and the name, origin and specification of the goods, are treated as strings. In addition, strings have some of their own characteristics. Therefore, the string as a data structure to study.

The basic concept of a string:

String (String)A finite sequence consisting of n (n≥ 0) characters. Generally recorded as: S= "c1c2...cn" (n≥01≤i≤n) can be letters, numbers or other characters, n is the length of the string, when n=07  43. If the lengths of the two strings are equal and the characters in the corresponding positions are equal, the two strings are called equal. In C #, compare two strings for equality and see a string of language and culture information. 

Storage and code implementations of the string:

Because the characters in the string are stored continuously, and in C # The string has a constant character, that is, once the strings are created, they cannot be longer, shorter, or alter any of them. Therefore, there is no discussion of chained storage of strings, nor interfaces to represent the operation of strings. Similarly, the string is treated as a class, and the class name is Stringds. The name Stringds is to be distinguished from the string class strings of C # itself. The class Stringds has only one field, which is the array data that holds the sequence of characters in the string. Because of the many operations of the string, the class Stringds contains only a subset of the basic operations. Methods and properties in the String class Stringds:

//Indexer     Public Char  This[intIndex]//ask for a string length Public intGetLength ()//string comparison Public intCompare (Stringds s)//To find a child string PublicStringds SubString (intIndexintlen)//String Connection PublicStringds Concat (stringds s)//string Insertion PublicStringds Insert (intindex, Stringds s)//string Delete PublicStringds Delete (intIndexintlen)//string Positioning Public intIndex (Stringds s)

Strings in C #:

In C #, a String represents a constant set of character sequences. The String type is a closed type, so it cannot be inherited by another class, and it inherits directly from theObject。 Therefore, String is a reference type, not a value type, and allocates space on the managed heap instead of on the thread's stack. The String type also inherits IComparable, ICloneable, IConvertible, icomparable<string>, ienumerable<Char>, IEnumerable and iequatable<string>and other interfaces. The constant character of string means that once a string is created, it cannot be longer, shorter, or change any of its characters. Therefore, when we operate on a string, we cannot change the string, as in the Stringds class defined in this book, the result of such operations as String joins, String insertions, and string deletions is to generate a new string without altering the original string. C # also provides a StringBuilder type to support the efficient dynamic creation of strings. In C #, creating a string is not availableNewoperator, instead of using a mechanism called string residency. This is because the C # language considers String as a primitive type. Primitive types are types that are directly supported by the compiler and can be used to directly express strings in the source code using literal constants (Literal). When the C # compiler compiles the source code, the literal constant string is stored in the metadata of the managed module. When the CLR initializes, the CLR creates an empty hash table, where the key is a string, and the value is a reference to the string object in the managed heap. Hash lists are hash tables. When the JIT compiler compiles a method, it looks for each literal constant string in the hash table. If it is not found, a new string object (pointing to a string) is constructed in the managed heap and then the string and a reference to the string object are added to the hash table, and no action is taken if it is found. 

Arrays in C # :

array is a kind of common data structure, which can be regarded as the generalization of linear table. An array is a data structure that is characterized by the fact that the elements in the structure can be data with some structure, even arrays, but belong to the same data type. Arrays are used as fixed types in many high-level languages. An array isafinite sequence of data elements of the same data type N (n≥ 1). A one-dimensional array can be thought of as a linear table, and a two-dimensional array can be thought of as a one-dimensional array of "data elements are one-dimensional arrays", and three-dimensional arrays can be thought of as one-dimensional arrays of "data elements are two-dimensional arrays" and so on. C # supports one-dimensional arrays, multidimensional arrays, and jagged arrays (arrays of arrays). All array types are implicitly inherited from System.Array. An Array is an abstract class that itself inherits from System.Object. Therefore, the array always allocates space on the managed heap, which is the reference type. Any array variable contains a reference to an array, not the array itself. When the value type of an element in an array, the memory space required by that type is also allocated as part of the array, and when the element of the array is a reference type, the array contains a reference only. 

Common methods in the array class:

usingSystem;usingSystem.Collections; Public Abstract classarray:icloneable, IList, ICollection, ienumerable{//determines whether the Array has a fixed size.      Public BOOLisfixedsize {Get; }//gets the number of Array elements.      Public intLength {Get; }//Gets the rank (number of dimensions) of the Array.      Public intRank {Get; }//implements the IComparable interface to search for a specific element in an. Array.      Public Static intBinarySearch (array array,Objectvalue);//implements a icomparable<t> generic interface that searches for specific elements in an Array.      Public Static intBinarysearch<t>(t[] array, T value);//implement the IComparable interface to search for values in a range of Array.      Public Static intBinarySearch (array array,intIndex,intLengthObjectvalue);//implements a icomparable<t> generic interface that searches for values in an Array.      Public Static intBinarysearch<t>(t[] array,intIndexintlength, T value);//The Array is set to zero, false, or null, depending on the element type.      Public Static voidClear (array array,intIndexintlength);//System.Array A shallow copy of the table.      Public ObjectClone ();//copies a series of elements in an Array starting from the first element//to another Array (starting with the first element).      Public Static voidCopy (array sourcearray, array destinationarray,intlength);//copies all the elements of a one-dimensional array to the specified one-dimensional array.      Public voidCopyTo (array array,intindex);//creates a multidimensional Array with a zero-based index, a specified Type, and a dimension length.      Public StaticArray CreateInstance (Type ElementType,params int[] lengths);//returns Arrayienumerator.      PublicIEnumerator GetEnumerator ();//gets the number of elements in the specified dimension of the Array.      Public intGetLength (intdimension);//gets the value of the specified position in a one-dimensional Array.      Public ObjectGetValue (intindex);//returns the index of the first occurrence in the entire one-dimensional Array.      Public Static intIndexOf (array array,Objectvalue);//returns the entire. The index of the first occurrence in an Array.      Public Static intIndexof<t>(t[] array, T value);//returns the index of the last occurrence in the entire one-dimensional Array.      Public Static intLastIndexOf (array array,Objectvalue);//reverses the order of elements in an entire one-dimensional Array.      Public Static voidReverse (array array);//the element that is set to the specified position in a one-dimensional Array.      Public voidSetValue (ObjectValueintindex);//sorts the elements in an entire one-dimensional Array.      Public Static voidSort (array array);}

Exercises:

1Set s= "I am a teacher", i= "excellent", r="Student". Use the method in the Stringds class to: (1) The length of the string s, I, R;2) S.substring (8,7), I.substring (2,1); (3) S.indexof ("Tea"), I.indexof ("cell"), R.indexof ("Den"). 2the substitution operation of a string refers to the known string s, T, R, and R to replace all occurrences of the substring in s with the T equivalent. Write the algorithm named Replace. 3The following string is known: a= "This", f= "A smple" c= "good", d= "NE", b= "︼", g="is", s=a.concat (B.concat (a.substring (3,2)). (F.substring (2,7)) , T=f.replace (F.substring (3,6), C), U=c.substring (3,1). Concat (d), v=S.concat (B.concat (T.concat (B.concat (U))). Ask S,t,v,getlength (s), V.indexof (g), U.indexof (g) what each is. 4set the known two strings as: S1= "BC CAD CABCADF", s2="ABC". Try to find the length of two strings and determine if the S2 string is a substring of the S1 string, and if S2 is a substring of S1, indicate the starting position of S2 in S1. 5Known: s= "(XYZ) +*", t= "(x+z) *y", try to convert s to t using basic operations such as joins, substrings, and substitutions.

String and array

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.