Found 34488 Articles for Programming

C# program to remove a range of characters by index using StringBuilder

karthikeya Boyini
Updated on 22-Jun-2020 13:47:13

418 Views

Use the Remove() method to remove a range of characters by index.Let’s say you need to remove the last 5 characters from the following string −StringBuilder myStr = new StringBuilder("Framework");For that, set the Remove() method as −str.Remove(3, 4);The following is the complete code −Example Live Demousing System; using System.Text; public class Program {    public static void Main() {       StringBuilder myStr = new StringBuilder("Framework");       Console.WriteLine("Initial String: " + myStr);       // removing four characters       Console.Write("New string: ");       myStr.Remove(5, 4);       Console.WriteLine(myStr);    } }OutputInitial String: Framework New string: Frame

C# program to remove characters starting at a particular index in StringBuilder

Samual Sam
Updated on 22-Jun-2020 13:35:12

72 Views

Set StringBuilder −StringBuilder str = new StringBuilder("Airport");Let’s say you need to remove characters. For that, use the Remove() method, which removes a bunch of characters beginning with a particular index −str.Remove(3, 4);The above removes four characters beginning from 3rd index (i.e. 4th position) −Here is the complete code −Example Live Demousing System; using System.Text; public class Program {    public static void Main() {       StringBuilder str = new StringBuilder("Airport");       Console.WriteLine("String: "+str);       // removing four characters       Console.Write("String after removing characters: ");       str.Remove(3, 4);       Console.WriteLine(str);    } }OutputString: Airport String after removing characters: Air

C# Program to change a character from a string

karthikeya Boyini
Updated on 22-Jun-2020 13:35:34

217 Views

Let's say our string is −StringBuilder str = new StringBuilder(); str.Append("pre");To change a character, set the value at that particular index. The following sets a character at the 3rd position −str[2] = 'o';Here is the complete code −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       StringBuilder str = new StringBuilder();       str.Append("pre");       Console.WriteLine("String : "+str);       Console.WriteLine("Change 3rd character");       str[2] = 'o';       Console.WriteLine("New String : "+str);    } }OutputString : pre Change 3rd character New String : pro

Access a character in C# StringBuilder

Samual Sam
Updated on 22-Jun-2020 13:35:57

313 Views

Firstly, set the StringBuilder −StringBuilder str = new StringBuilder(); str.Append("premium");To access the 5th character and display it −Console.WriteLine(str[4]);The following is the complete code −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       StringBuilder str = new StringBuilder();       str.Append("premium");       Console.WriteLine("String : "+str);       Console.Write("Accessing 5th character : ");       Console.WriteLine(str[4]);    } }OutputString : premium Accessing 5th character : i

Clear a StringBuilder in C#

karthikeya Boyini
Updated on 22-Jun-2020 13:36:36

2K+ Views

To clear a StringBuilder, use the Clear() method.Let’s say we have set the following StringBuilder −string[] myStr = { "One", "Two", "Three", "Four" }; StringBuilder str = new StringBuilder("We will print now...").AppendLine();Now, use the Clear() method to clear the StringBuilder −str.Clear();Let us see the complete code −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       // string array       string[] myStr = { "One", "Two", "Three", "Four" };       StringBuilder str = new StringBuilder("We will print now...").AppendLine();       // foreach loop to append ... Read More

Iterating C# StringBuilder in a foreach loop

Samual Sam
Updated on 22-Jun-2020 13:37:06

2K+ Views

Firstly, set a string array and StringBuilder −// string array string[] myStr = { "One", "Two", "Three", "Four" }; StringBuilder str = new StringBuilder("We will print now...").AppendLine();Now, use foreach loop to iterate −foreach (string item in myStr) {    str.Append(item).AppendLine(); }The following is the complete code −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       // string array       string[] myStr = { "One", "Two", "Three", "Four" };       StringBuilder str = new StringBuilder("We will print now...").AppendLine();       // foreach loop to append ... Read More

Compare the content of two StringBuilders

karthikeya Boyini
Updated on 22-Jun-2020 13:38:04

2K+ Views

Equals method is used in C# to compare the content of two StringBuilders.The following are our two StringBuilders −// first StringBuilder str1 = new StringBuilder(); str1.Append("Tim"); str1.Append("Tom"); str1.Append("Henry"); // second StringBuilder str2 = new StringBuilder(); str2.Append("John"); str2.Append("David"); str2.Append("Beth");Now use the Equals() method to compare both the methods −if (str1.Equals(str2)) {    Console.WriteLine("Contents are equal!"); }The following is the complete code −Example Live Demousing System; using System.Text; class Demo {    static void Main() {       // first       StringBuilder str1 = new StringBuilder();       str1.Append("Tim");       str1.Append("Tom");       str1.Append("Henry"); ... Read More

Replace a string using StringBuilder

Samual Sam
Updated on 22-Jun-2020 13:38:40

528 Views

Set a String −StringBuilder str = new StringBuilder("Fitness is important");Use the Replace() method to replace a string −str.Replace("important", "essential");The following is the code to replace a string using StringBuilder −Example Live Demousing System; using System.Text; class Demo {    static void Main() {       // Initial String       StringBuilder str = new StringBuilder("Fitness is important");       Console.WriteLine(str.ToString());       // Replace       str.Replace("important", "essential");       // New String       Console.WriteLine(str.ToString());       Console.ReadLine();    } }OutputFitness is important Fitness is essential

Append to StringBuilder in C# with a new line on the end

karthikeya Boyini
Updated on 22-Jun-2020 13:39:07

3K+ Views

The AppendLine() method appends the content and add a new line on the end.Firstly, set the StringBuilder −StringBuilder str = new StringBuilder();Use AppendLine() −str.AppendLine("Accessories"); str.AppendLine(); str.AppendLine("Electronics");The following is the complete code −Example Live Demousing System; using System.Text; class Demo {    static void Main() {       StringBuilder str = new StringBuilder();       str.AppendLine("Accessories");       str.AppendLine();       str.AppendLine("Electronics");       Console.Write(str);    } }OutputAccessories Electronics

Append to StringBuilder in C#

Samual Sam
Updated on 22-Jun-2020 13:39:34

414 Views

The Append() method add content to a StringBuilder.Set a String −StringBuilder str = new StringBuilder();Now, loop through the number of elements you want and use Append () to append to StringBuilder −for (int j = 0; j < 5; j++) {    str.Append(j).Append(" "); }The following is the complete code −Example Live Demousing System; using System.Text; class Program {    static void Main() {       StringBuilder str = new StringBuilder();       for (int j = 0; j < 5; j++) {          str.Append(j).Append(" ");       }       Console.WriteLine(str);    } }Output0 1 2 3 4

Advertisements