Uses the Length attribute of an object to crop the object content.

Source: Internet
Author: User
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.

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.