Replace a String Using StringBuilder

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

810 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

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

Iterate 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

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

Access a Character in C# StringBuilder

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

443 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

Change a Character in a String using C#

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

330 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

Remove Characters Starting at a Particular Index in StringBuilder

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

152 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

Create a New Database Using PHP Script

Nancy Den
Updated on 22-Jun-2020 13:34:56

173 Views

As we know that PHP provides us the function named mysql_query to create a new database.ExampleTo illustrate this we are creating a database named ‘Tutorials’ with the help of PHP script in the following example −           Creating MySQL Database                  

Purpose of System Programs

Kristi Castro
Updated on 22-Jun-2020 13:34:43

8K+ Views

System programs provide an environment where programs can be developed and executed. In the simplest sense, system programs also provide a bridge between the user interface and system calls. In reality, they are much more complex. For example, a compiler is a complex system program.System Programs PurposeThe system program serves as a part of the operating system. It traditionally lies between the user interface and the system calls. The user view of the system is actually defined by system programs and not system calls because that is what they interact with and system programs are closer to the user interface.An ... Read More

Select MySQL Database Using PHP Script

Vrundesha Joshi
Updated on 22-Jun-2020 13:33:45

210 Views

As we know that PHP provides us the function named mysql_select_db to select a mysql database.ExampleTo illustrate this we are selecting a database named ‘Tutorials’ with the help of PHP script in the following example −           Selecting MySQL Database                  

Advertisements