When using the JScript array, I found a very interesting feature, that is, its built-in object array length can be written. Strange, right? But it's fun. So what will happen if I change its length?
When the Length attribute of the array instance is modified, the size of the array is changed.
For example, the array of JScript: var array = ['A', 'B', 'C'];
We make array. Length = 1, and the elements in the array become: ['a'];
If array. length is set to 5, the elements in the array are changed to: ['A', 'B', 'C', '',''].
// Operations are performed relative to the original array.
In addition to JScript, this feature is also available for instances of the Bcl stringbuilder class in. NET Framework.
Example of stringbuilder: stringbuilder strb = new stringbuilder ("ABC ");
Let's change strb. Length = 1, the object content to: "";
If strb. Length = 5 is set, the object content is changed to "ABC ";
When the new lenght is smaller than the length of strb itself, the object content is truncated according to the specified length. If the new length is greater than the length of strb itself, the system uses spaces to fill in the insufficiency.
Using this feature of stringbuilder, we can also use it to implement a function for obtaining a specified number of space strings:
String getspaces (INT count)
{
Stringbuilder strb = new stringbuilder (count );
Strb. Length = count;
Return strb. tostring ();
}
// Efficient and refreshing ~~
Of course, the stringbuilder feature I use is usually used when I have something like: "A, B, C, D, E," or "1; 2; 3; 4; 5; "this string, and want to get:" A, B, C, D, E "or" 1; 2; 3; 4; 5. Use strb. Length = strb. Length-1.
Here is a tip. If our string is not in stringbuilder format but in string format, we 'd better not generate: "A, B, C, D, E," or "1; 2; 3; 4; 5. In this case, we need to use STR = Str. substring (0, str. length-1) to truncate the string. If the format generated in the loop is: ", a, B, c, d, e" or "; 1; 2; 3; 4; 5 ". It is easier to obtain "a, B, c, d, e" or "1; 2; 3; 4; 5", directly STR = Str. substring (1.