String operations> dynamic stringbuilder

Source: Internet
Author: User

Dynamic stringbuilder>

The length of the dynamic stringbuilder can be dynamically changed during running. You can specify a certain length for stringbuilder. If the capacity exceeds the specified length limit, stringbuilder will apply for more space.

 

Dynamic string stringbuilder> set stringbuilder capacity

Code Using system;
Using system. Collections. Generic;
Using system. text;

Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
Stringbuilder SB1 = new stringbuilder ("stringbuilder exercises ");
// The default stringbuilder initialization length is 16 characters
String outstr = "demo text:" + sb1.tostring () + environment. newline + "Length:" + sb1.length + environment. newline + "Capacity:" + sb1.capacity;
// Make sure the SB1 capacity is 80.
Sb1.ensurecapacity (80 );
Outstr + = environment. newline + "the current capacity using ensurecapacity is:" + sb1.capacity;
Sb1.capacity = 60;
Outstr + = environment. newline + "current capacity:" + sb1.capacity;
Console. writeline (outstr );
Console. Readline ();
}
}
}

Dynamic stringbuilder> append operation

Append and appendformat Methods Using system;
Using system. Collections. Generic;
Using system. text;

Namespace appendandappendformat
{
Class Program
{
Static void main (string [] ARGs)
{
Stringbuilder sb = new stringbuilder ();
String str1 = ",";
Char [] char2 = {'>', '>', '> '};
Bool strbool = false;
Int I = 13;
// Append: append a specified string type to the end of the stringbuilder object instance.
SB. append (str1 );
SB. append (char2 );
SB. append (strbool );
SB. append (I );
SB. appendline ();
// Appendformat provides the function of appending a formatted string to the end of the stringbuilder object instance.
SB. appendformat ("string value {0}", str1 );
SB. appendformat ("character array {0}", char2 );
SB. appendline ();
SB. appendformat ("Boolean value {0}", strbool );
SB. appendformat ("integer value {0: d}", I );
Console. writeline (sb. tostring ());
Console. Readline ();
}
}
}

Dynamic string stringbuilder> insert operation

The append operation can only append to the end of stringbuilder, while the insert operation can specify the index position to insert.

Insert Method Using system;
Using system. Collections. Generic;
Using system. text;

Namespace insert
{
Class Program
{
Static void main (string [] ARGs)
{
Stringbuilder sb = new stringbuilder ();
SB. insert (0, false );
SB. insert (1, "This is a string value ");
SB. insert (1, 2,123 );
SB. insert (4, new char [] {'1', '2', '3 '});
Console. writeline (sb. tostring ());
Console. Readline ();
}
}
}

Dynamic stringbuilder> delete operation

Remove Method Using system;
Using system. Collections. Generic;
Using system. text;

Namespace remove
{
Class Program
{
Static void main (string [] ARGs)
{
Stringbuilder sb = new stringbuilder ("this is a string to be removed ");
Stringbuilder sb2 = sb. Remove (0, 2 );
Console. writeline (sb2.tostring ());
Console. readkey ();

}
}
}

Dynamic string stringbuilder> replace

Replace Method Using system;
Using system. Collections. Generic;
Using system. text;

Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
Stringbuilder sb = new stringbuilder ("My name is Jin sanshun ");
Console. writeline (sb. Replace ("I", "you "));
Console. writeline (sb. Replace ("Jin sanshun", "Wu bisheng "));
// The first parameter is the old string, the second parameter is the new string, the third parameter is the index position, and the fourth parameter is the length of the new string.
Console. writeline (sb. Replace ("name", "name", 0, SB. Length ));
Console. writeline (sb. Replace ("called", "yes ));
Console. Readline ();

}
}
}

 

 

 

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.