Use js to implement the stringbuilder effect of C ~

Source: Internet
Author: User
/*
##################### Do not modify this header ############## #######
# Title: stringbuilder class #
# Description: simulates the C # stringbuilder class in Javascript .#
# Website: www.codevendor.com #
# Author: Adam Smith #
# Email: ibulwark@hotmail.com #
# Date: November 12,200 6 #
######################################## #############################
*/

// Simulates the C # stringbuilder class in JavaScript.
// Parameter ["stringtoadd"]-the string to add.
Stringbuilder =   Function (Stringtoadd)
{
VaR H =   New Array ();
If (Stringtoadd) {H [ 0 ] = Stringtoadd ;}
This . Append = Append;
This . Appendline = Appendline;
This . Tostring = Tostring;
This . Clear = Clear;
This . Length = Length;
This . Replace = Replace;
This . Remove = Remove;
This . Insert = Insert;
This . GetType = GetType;

// Appends the string representation of a specified object to the end of this instance.
// Parameter ["stringtoappend"]-the string to append.
Function Append (stringtoappend)
{
H [H. Length] = Stringtoappend;
}

// Appends the string representation of a specified object to the end of this instance with a carriage return and line feed.
// Parameter ["stringtoappend"]-the string to append.
Function Appendline (stringtoappend)
{
H [H. Length] = Stringtoappend;
H [H. Length] =   " \ R \ n " ;
}

// Converts a stringbuilder to a string.
Function Tostring ()
{
If ( ! H ){ Return   "" ;}
If (H. Length < 2 ){ Return (H [ 0 ]) ? H [ 0 ]: "" ;}
VaR A = H. Join ( '' );
H =   New Array ();
H [ 0 ] = A;
Return A;
}

//Clears the stringbuilder
FunctionClear ()
{
H= NewArray ();
}

// Gets the stringbuilder Length
Function Length ()
{
If ( ! H ){ Return   0 ;}
If (H. Length < 2 ){ Return (H [ 0 ]) ? H [ 0 ]. Length: 0 ;}
VaR A = H. Join ( '' );
H =   New Array ();
H [ 0 ] = A;
Return A. length;
}

// Replaces all occurrences of a specified character or string in this instance with another specified character or string.
// Parameter ["oldvalue"]-the string to replace.
// Parameter ["newvalue"]-The string that replaces oldvalue.
// Parameter ["casesensitive"]-True or false for case replace.
// Return Value-a reference to this instance with all instances of oldvalue replaced by newvalue.
Function Replace (oldvalue, newvalue, casesensitive)
{
VaR R =   New Regexp (oldvalue, (casesensitive = True ) ? ' G ' : ' Gi ' );
VaR B = H. Join ( '' ). Replace (R, newvalue );
H =   New Array ();
H [ 0 ] = B;
Return   This ;
}

// Removes the specified range of characters from this instance.
// Parameter ["startindex"]-the position where removal begins.
// Parameter ["length"]-the number of characters to remove.
// Return Value-a reference to this instance after the Excise Operation has occurred.
Function Remove (startindex, length)
{
VaR S = H. Join ( '' );
H =   New Array ();

If (Startindex < 1 ) {H [ 0 ] = S. substring (length, S. Length );}
If (Startindex > S. Length) {H [ 0 ] = S ;}
Else
{
H [ 0 ] = S. substring ( 0 , Startindex );
H [ 1 ] = S. substring (startindex + Length, S. Length );
}

Return   This ;
}

// Inserts the string representation of a specified object into this instance at a specified character position.
// Parameter ["Index"]-the position at which to insert.
// Parameter ["value"]-the string to insert.
// Return Value-a reference to this instance after the insert operation has occurred.
Function Insert (index, value)
{
VaR S = H. Join ( '' );
H =   New Array ();

If (Index < 1 ) {H [ 0 ] = Value; H [ 1 ] = S ;}
If (Index > = S. Length) {H [ 0 ] = S; H [ 1 ] = Value ;}
Else
{
H [ 0 ] = S. substring ( 0 , Index );
H [ 1 ] = Value;
H [ 2 ] = S. substring (index, S. Length );
}

Return   This ;
}

//Gets the type
FunctionGetType ()
{
Return "Stringbuilder";
}
};

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.