Adv C#
Programming

1. Introduction to .NET and C# 2. Points to keep in mind for pyramid 3. C# Pyramid Program Example for type 1 4. C# Pyramid Program Example for type 2 5. C# Pyramid Programs on Type 3 6. C# Pyramid Programs of type 4 7. C# Pyramid Programs of type 5 8. Classes and Objects 9. Recursive functions 10. Constructors in C# 11. Destructor 12. Method Overloading 13. Constructor Overloading 14. Inheritance in C# 15. Interface in C# 16. Method Overriding 17. readonly and sealed keyword in C# 18. Exception Handling 19. Delegates 20. Multicast Delegates 21. Events 22. Structure 23. Threads 24. Working with Threads 25. Thread Life Cycle 26. Introducing Multithreading 27. Advantage and limitation of multithreading 28. Thread priority 29. Properties 30. Collections 31. ArrayList Class 32. Hastable Class 33. SortedList 34. Stack 35. Queue Class 36. Generics 37. Unsafe code 38. C# Networking 39. Prgs for Science and Engineer 40. Prg to use alias 41. Prg to count minors and majors using array 42. Prg for sum up of two array in third array 43. Prg to show boxing and unboxing 44. Prg for type casting from byte to int 45. Prg to find area of circle 46. Prg to find cube root of entered number 47. Prg for digit manipulation 48. Prg for printing a reverse triangle 49. Prg to show case and goto fall through 50. Prg to show use of foreach loop 51. Prg for adding 2 numbers 52. Prg to show jagged array 53. Prg to show different methods of Array 54. Prg to print number Pyramid 55. Prg to show entered string is palindrome or not 56. Prg to show entered number is prime or not 57. Prg to show different methods of StringBuilder 58. Prg to show different methods of String 59. Prg for appending a string using StringBuilder 60. Prg to show appending a string with numeric type data using StringBuilder 61. Prg to show use of ArrayList 62. Prg to pass an array as a parameter to a function for finding out a prime number 63. Prg to show the constant member 64. Prg for showing destructor 65. Prg for sum up and reversing the entered number 66. Prg for enum type conversation 67. Prg to show sum of first fifty even and odd number 68. Prg to find the factorial of entered number 69. Prg for iterating each element of array using foreach loop 70. Prg to show array position and value 71. Prg to show how to insert and remove string using StringBuilder 72. Prg to show how to join the string 73. Prg to show the value of sin 90 74. Prg to enter two number and find max 75. Prg to show padding 76. Prg to show entered number is prime or not 77. Prg to show private constructor 78. Prg to show how to use getter and setter property 79. Prg using out parameter 80. Prg to count the number of spaces and words in a String 81. Prg to show how to use Split method 82. Prg to create binary triangle 83. Prg to create dule star triangle 84. Prg to create triangle 85. Prg to show use of EndsWith and StartsWith method of string 86. Prg for static constructor 87. Prg to show how to use insert method 88. Prg to sort a string 89. Prg to store student name and roll no using structure 90. Prg to show how to use delegate 91. Prg to calculate depreciation 92. Prg for calculating Economic order cost and time between order 93. Prg to find radius of circle 94. Prg to sum up odd numbers from 0 to 20 95. Prg to calculate simple interest 96. Prg to categorise student on base of there marks range where marks are stored in array 97. Prg to create triangle on console screen 98. Prg to print sin theta, cos theta and tan theta from 0.1 to 1.0 angle in radians 99. Prg to demonstrate variables 100. Prg to illustrate the differences between int and double 101. Prg to show exception handling using try.. catch block 102. Prg to demonstrate how to use interface 103. Prg to print number triangle 104. Prg to demonstrate structure 105. Prg to test exception 106. Prg to count number of spaces and words in a entered string 107. Prg to demonstrate ref parameter 108. Prg to print a matrix table from 1 to 10 109. Prg to display 2 D array elements position in matrix form 110. Prg to count number of adult and children 111. Prg to print pascal triangle 112. Prg to demonstrate stringbuilder and its method 113. Prg to reverse a string 114. Prg for string sorting 115. Interview Question & Answer part 1 116. Interview Question & Answer part 2 117. Interview Question & Answer part 3 118. Interview Question & Answer part 4 119. Interview Question & Answer part 5 120. Interview Question & Answer part 6 121. Interview Question & Answer part 7 122. Interview Question & Answer part 8 123. Interview Question & Answer part 9 124. Interview Question & Answer part 10 125. Interview Question & Answer part 11 126. Interview Question & Answer part 12 127. Interview Question & Answer part 13 128. Interview Question & Answer part 14 129. Interview Question & Answer part 15
Pr.Pg Next Pg

C# program tutorials to show different methods of StringBuilder

 

Example: C# program to show different methods of StringBuilder

 

// C# program to show different methods of StringBuilder

using System;

using System.Text;

 

class StringBuilderDemo

{

public static void Main()

{

StringBuilder sb1 = new StringBuilder("Hello");

StringBuilder sb2 = new StringBuilder();

StringBuilder sb3 = sb1;

char[] ch ={ 'a', 'b', 'c' };

char[] ch1 ={ 'm', 'p' };

char[] ch2 ={ 'm', 'c' };

//1st method in stringbiulder appends accepts 2 arguments //character value and repeat count.

sb2 = sb1.Append('a',3 );

//Helloaaa

Console.WriteLine("StringBuilder Append 1st Method. StringBuilder.append(char. value,repeat count.");

Console.WriteLine("it returns original string + charater value repeated repeat count times.");

 

Console.WriteLine("Original String. : Hello" );

Console.WriteLine(sb2);

 

//2nd method

//it accepts 3 arguments. character array which is (a,b,c), //integer startindex,integer char.count

Console.WriteLine("2nd Method. Stringbuilder.append(char array ch ,startindex m,char. count n).");

 

Console.WriteLine("It returns original string+ n characters "+

"of char array ch starting from m th position.");

 

sb2 = sb1.Append(ch, 1, 2);

Console.WriteLine(sb1); //hellobc

Console.WriteLine(sb2); //hellobc

//3rd method it accepts string value as an argument and returns //appended string

Console.WriteLine("StringBuilder Append 3rd Method. "+ "stringbuilder.append(string value)" );

sb1=new StringBuilder("Hello");

sb2 = sb1.Append("David");

Console.WriteLine(sb1); //helloDavid

Console.WriteLine(sb2); //helloDavid

 

//4th Method it accepts it accepts string value,start pos,char. count and

Console.WriteLine("StringBuilder Append 4th Method. " +

"It accepts string value,start index and no. of char.count,");

sb1=new StringBuilder("Hello");

sb2 = sb1.Append( "David", 2, 3);

Console.WriteLine(sb1); //Hellovid

Console.WriteLine(sb2); //Hellovid

 

//5th Method it accepts char value. and appends it to original string.

Console.WriteLine("StringBuilder Append 5th Method. "+

"It accepts char value and join it to original string.");

sb1=new StringBuilder("Hello");

sb2 = sb1.Append('J');

Console.WriteLine(sb1); //Helloj

Console.WriteLine(sb2); //Helloj

 

//6th Method.it accepts char array as an argument and returns //original string+char array elements.

Console.WriteLine("StringBuilder Append 6th Method");

Console.WriteLine("it accepts char array as an argument " +

" and returns original string+char array elements.");

sb1 = new StringBuilder("Hello");

sb2 = sb1.Append(ch1);

Console.WriteLine(sb1); // Hellomp

Console.WriteLine(sb2); // Hellomp

 

//7th Method. it accepts the position at which u //insert the //string,string to insert and number of times to insert

//The position at which to insert.

//value

//The string to insert.

//count

//The number of times to insert value.

Console.WriteLine("StringBuilder Append 7th Method");

Console.WriteLine("It accepts the position at which u " +

"insert the string,string to insert and number of times " +

"to insert");

sb1=new StringBuilder("Hello");

sb2 = sb1.Insert(3, "David", 1);

 

//index

Console.WriteLine(sb1); //HelDavidlo

Console.WriteLine(sb2); //HelDavidlo

 

//8th Method

//it accepts the pos at which to insert and the string to insert

Console.WriteLine("StringBuilder Append 8th Method");

sb1=new StringBuilder("Hello");

Console.WriteLine("it accepts the pos at which to insert "+

"and the string to insert");

sb2 = sb1.Insert(3, "Jack");

Console.WriteLine(sb1); //HelJacklo

Console.WriteLine(sb2); //HelJacklo

 

//9th Method

//It accepts the pos. at which to insert and the char array to //insert.

Console.WriteLine("StringBuilder Append 9th Method");

Console.WriteLine("It accepts the pos. at which to insert " +

"and the char array to insert.");

sb1=new StringBuilder("Hello");

sb2 = sb1.Insert(2, ch2);

Console.WriteLine(sb1); //Hemcllo

Console.WriteLine(sb2); //Hemcllo

 

//10th Method

Console.WriteLine("StringBuilder 10th Method");

//It accepts start pos and the no. of characters to remove.

Console.WriteLine("It accepts start pos and the no. " +

"of characters to remove.");

sb1=new StringBuilder("Hello");

sb2 = sb1.Remove(1, 3);

Console.WriteLine(sb1); //Ho

Console.WriteLine(sb2); //Ho

 

//11th Method return a boolean value indicating whether this //instance is eqal to the //specified object.

sb1=new StringBuilder("Hello");

Console.WriteLine("11th Method : returns a boolean "+

"Value indicating whether this ");

 

Console.WriteLine("instance is eqal to the specified object.");

bool t = sb1.Equals(sb3);

Console.WriteLine(t);

 

//12th Method It returns capacity.It means the maximam no. of //characters that can be //contained in the memory alloted by the current //instance.

Console.WriteLine("12th Method :It returns capacity.It " +

"means the maximam no. of characters ");

Console.WriteLine("that can be contained in the memory " +

"alloted by the current instance.");

 

StringBuilder sb4 =new StringBuilder("Hello");

int c = sb4.Capacity;

Console.WriteLine("StringBuilder Capacity : {0}",c);

int m=sb4.Length;

Console.WriteLine("The Length of the string Hello is : {0}", m);

 

//13th Method It

// Replaces all occurrences of a specified character or string // in this instance

// with another specified character or string.

Console.WriteLine("13 Method : It replaces all occurences of " +

"the specified characters that can be contained in the " +

"memory allotted by the current instance.");

StringBuilder sb5 = new StringBuilder("Jack & Jill.");

sb5.Replace("&","*");

Console.WriteLine("Old string = Jack & jill. The new " +

"string after replacing & with * is {0} ",sb5);

 

}

}

Output

 

StringBuilder Append 1st Method. StringBuilder.append(char. value,repeat count.

it returns original string + charater value repeated repeat count times.

Original String. : Hello

Helloaaa

2nd Method. Stringbuilder.append(char array ch ,startindex m,char. count n).

It returns original string+ n characters of char array ch starting from m th position.

Helloaaabc

Helloaaabc

StringBuilder Append 3rd Method. stringbuilder.append(string value)

HelloDavid

HelloDavid

StringBuilder Append 4th Method. It accepts string value,start index and no. of char.count,

Hellovid

Hellovid

StringBuilder Append 5th Method. It accepts char value and join it to original string.

HelloJ

HelloJ

StringBuilder Append 6th Method

it accepts char array as an argument and returns original string+char array elements.

Hellomp

Hellomp

StringBuilder Append 7th Method

It accepts the position at which u insert the string,string to insert and number of times to insert

HelDavidlo

HelDavidlo

StringBuilder Append 8th Method

it accepts the pos at which to insert and the string to insert

HelJacklo

HelJacklo

StringBuilder Append 9th Method

It accepts the pos. at which to insert and the char array to insert.

Hemcllo

Hemcllo

StringBuilder 10th Method

It accepts start pos and the no. of characters to remove.

Ho

Ho

11th Method : returns a boolean Value indicating whether this

instance is eqal to the specified object.

False

12th Method :It returns capacity.It means the maximam no. of characters

that can be contained in the memory alloted by the current instance.

StringBuilder Capacity : 16

The Length of the string Hello is : 5

13 Method : It replaces all occurences of the specified characters that can be contained in the memory allotted by the current instance.

Old string = Jack & jill. The new string after replacing & with * is Jack * Jill

 

Pr.Pg border                                              Next Pg