Home Web Front-end JS Tutorial js implements C#'s StringBuilder effect complete example_javascript skills

js implements C#'s StringBuilder effect complete example_javascript skills

May 16, 2016 pm 03:24 PM
c# js stringbuilder

The example in this article describes how js implements the StringBuilder effect of C#. Share it with everyone for your reference, the details are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

/*

  ##################### DO NOT MODIFY THIS HEADER #####################

  # Title: StringBuilder Class                    #

  # Description: Simulates the C# StringBuilder Class in Javascript. #

  # Author: Adam Smith                        #

  # Email: ibulwark@hotmail.com                    #

  # Date: November 12, 2006                      #

  #####################################################################

*/

// 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])&#63;h[0]:""; }

    var a = h.join('');

    h = new Array();

    h[0] = a;

    return a;

  }

  // Clears the StringBuilder

  function Clear()

  {

    h = new Array();

  }

  // Gets the StringBuilder Length

  function Length()

  {

    if(!h){return 0;}

    if(h.length<2){ return (h[0])&#63;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)&#63;'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

  function GetType()

  {

    return "StringBuilder";

  }

};

Copy after login

I hope this article will be helpful to everyone in JavaScript programming.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Access Modifiers in C# Access Modifiers in C# Sep 03, 2024 pm 03:24 PM

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

See all articles