C # string and Array Operations)

Source: Internet
Author: User
String operation (take the current time)
String time = convert. tostring (datetime. Today). Split (New char [] {''}); textbox1.text = time [0]; use space as the demarcation point;
Array Overview
C # create an index for the array from scratch, that is, the array index starts from scratch. In C #, arrays work in a similar way as in most other popular languages. However, some differences should be noted.
When declaring an array, square brackets ([]) must follow the type, not the identifier. In C #, placing square brackets after identifiers is invalid syntax.
Int [] Table; // not int table [];
Another detail is that the size of an array is not a part of its type, but in C, it is a part of the array type. This allows you to declare an array and assign it any array of the int object regardless of the array length.
Int [] numbers; // declare numbers as an int array of any size
Numbers = new int [10]; // numbers is a 10-element array
Numbers = new int [20]; // now it's a 20-element array
Declare an array
C # supports one-dimensional arrays, multi-dimensional arrays (rectangular arrays), and arrays (staggered arrays ). The following example shows how to declare arrays of different types:
One-dimensional array: int [] numbers;
Multi-dimensional array: String [,] names;
Array array (staggered): byte [] [] scores;
Declaring arrays (as shown above) does not actually create them. In C #, arrays are objects (discussed later in this tutorial) and must be instantiated. The following example shows how to create an array:
One-dimensional array: int [] numbers = new int [5];
Multi-dimensional array: String [,] names = new string [5, 4];
Array array (staggered): byte [] [] scores = new byte [5] []; for (INT x = 0; x <scores. length; X ++) {scores [x] = new BYT [4];
}
You can also have a larger array. For example, you can have a three-dimensional array of rectangles: int [,] buttons = new int [, 3];
You can even mix a rectangle array with a staggered array. For example, the following code declares the one-dimensional array int [] [,] [,] numbers of a two-dimensional array of Type 'int;
Initialize an array
C # It provides a simple and straightforward method to initialize the array during declaration by including the initial value in braces. The following example shows how to initialize arrays of different types.
Note: If the array is not initialized at the time of declaration, the array members will be automatically initialized to the default initial value of the array type. In addition, if an array is declared as a field of a certain type, it is set to the default value null when it is instantiated.
One-dimensional array
Int [] numbers = new int [5] {1, 2, 3, 4, 5 };
String [] names = new string [3] {"Matt", "Joanne", "Robert "};
The array size can be omitted, as shown below:
Int [] numbers = new int [] {1, 2, 3, 4, 5 };
String [] names = new string [] {"Matt", "Joanne", "Robert "};
If you provide an initial value setting item, you can also omit the new operator, as shown below:
Int [] numbers = {1, 2, 3, 4, 5 };
String [] names = {"Matt", "Joanne", "Robert "};
Multi-dimensional array
Int [,] numbers = new int [3, 2] {1, 2}, {3, 4}, {5, 6 }};
String [,] siblings = new string [2, 2] {"Mike", "Amy" },{ "Mary", "Albert "}};
The array size can be omitted, as shown below:
Int [,] numbers = new int [,] {1, 2}, {3, 4}, {5, 6 }};
String [,] siblings = new string [,] {"Mike", "Amy" },{ "Mary", "Albert "}};
If you provide an initial value setting item, you can also omit the new operator, as shown below:
Int [,] numbers = {1, 2}, {3, 4}, {5, 6 }};
String [,] siblings = {"Mike", "Amy" },{ "Mary", "Albert "}};
Staggered array (array)
You can initialize the staggered array as shown in the following example:
Int [] [] numbers = new int [2] [] {New int [] {2, 3, 4}, new int [] {5, 6, 7, 8, 9 }};
The size of the first array can be omitted, as shown below:
Int [] [] numbers = new int [] [] {New int [] {2, 3, 4}, new int [] {5, 6, 7, 8, 9};-or-
Int [] [] numbers = {New int [] {2, 3, 4}, new int [] {5, 6, 7, 8, 9 }};
Note that there is no initialization syntax for elements in the staggered array.
Member Groups
The members of the member number can be directly performed, similar to those of the member number in C/C ++. For example, the following code creates an array named numbers and assigns 5 to the fifth element of the array:
Int [] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
Numbers [4] = 5;
The following Code declares a multi-dimensional array and assigns 5 to members located in [1, 1:
Int [,] numbers = {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10 }};
Numbers [1, 1] = 5;
The following declares a one-dimensional staggered array, which contains two elements. The first element is an array of two integers, and the second element is an array of three integers:
Int [] [] numbers = new int [] [] {New int [] {1, 2}, new int [] {3, 4, 5 }};
The following statement assigns 58 to the first element of the first array and 667 to the second element of the second array:
Numbers [0] [0] = 58;
Numbers [1] [1] = 667;
Array is an object
In C #, arrays are actually objects. System. array is the abstract base type of all array types. You can use the attributes of system. array and other class members. In this example, the Length attribute is used to obtain the length of the array. The following Code assigns the numbers array length (5) to a variable named lengthofnumbers:
Int [] numbers = {1, 2, 3, 4, 5 };
Int lengthofnumbers = numbers. length;
The system. array class provides many other useful methods/attributes, such as methods for sorting, searching, and copying arrays.
Use foreach for Arrays
C # also provides the foreach statement. This statement provides a simple and clear method to cyclically access the elements of the array. For example, the following code creates an array named numbers and cyclically accesses the Array Using the foreach statement:
Int [] numbers = {4, 5, 6, 1, 2, 3,-2,-1, 0 };
Foreach (int I in numbers) {system. Console. writeline (I );}
With multidimensional arrays, you can use the same method to cyclically access elements. For example:
Int [,] numbers = new int [3, 2] {9, 99}, {3, 33}, {5, 55 }};
Foreach (int I in numbers) {console. Write ("{0}", I );}
The output in this example is 9 99 3 33 5 55
However, with multidimensional arrays, nested for loops allow you to better control array elements.
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.