What are the C # array and string operations?
This article mainly introduces "what is the operation of C # array and string". In the daily operation, I believe that many people have doubts about the operation of C # array and string. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the operations of C # array and string?" Next, please follow the editor to study!
About C # array and C # string operation:
1) A string is made up of continuously stored characters
2) the string in C# has a constant property, that is, once it is created, it cannot change the length or any of the characters.
3) string connection, insertion and deletion all generate a new string without changing the original string.
4) inherited from System.object. So it's a reference type (int,bool,char and so on are all struct, not class, but value types).
5) System.String is a sealed class, so it cannot be inherited.
6) although System.String is a reference type, String is treated as a primitive type in C #, so instead of the new operator to create an instance, the string resident mechanism is used.
7) System.String inherits from IComparable, ICloneable, IConvertible, IComparable, IEnumerable, IEnumerable, IEquatable.
8) C # provides the StringBuilder type to support efficient dynamic creation of strings.
The following is a custom string class that contains a field to hold the C # array of character sequences, as well as some common C # string operations.
Public class StringDS {private char [] data;//char array / / indexer public char this [int index] {get {return data [index];} set {data [index] = value;}} / / constructor public StringDS (char [] arr) {data = new char [arr.Length]; for (int I = 0; I
< arr.Length; i++) { data[i] = arr[i]; } } //构造函数 public StringDS(int len) { char[] arr = new char[len]; data = arr; } //求串长 public int GetLength() { return data.Length; } //串比较 public int Compare(StringDS s) { int len=((this.GetLength() this.GetLength() - 1) { Console.WriteLine("Position is error!"); return null; } for (int i = 0; i < index; ++i) { s1[i] = this[i]; } for(int i = index; i < index + len ; ++i) { s1[i] = s[i - index]; } for (int i = index + len; i < len2; ++i) { s1[i] = this[i - len]; } return s1; } //串删除 public StringDS Delete(int index, int len) { if ((index < 0) || (index >This.GetLength ()-1) | | (len
< 0) || (len >This.GetLength ()-index) {Console.WriteLine ("Position or Length is error!"); return null;} StringDS s = new StringDS (this.GetLength ()-len); for (int I = 0; I < index; + + I) {s [I] = this [I];} for (int I = index + len; I < this.GetLength (); + + I) {s [I] = this [I];} return s } / / string positioning public int Index (StringDS s) {if (this.GetLength () < s.GetLength ()) {Console.WriteLine ("There is not string s!"); return-1;} int I = 0; int len = this.GetLength ()-s.GetLength (); while (I < len) {if (this.Compare (s) = = 0) {break;}} if (I