Csharp Articles

Page 74 of 196

Compare the content of two StringBuilders

karthikeya Boyini
karthikeya Boyini
Updated on 22-Jun-2020 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

Iterating C# StringBuilder in a foreach loop

Samual Sam
Samual Sam
Updated on 22-Jun-2020 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
karthikeya Boyini
Updated on 22-Jun-2020 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
Samual Sam
Updated on 22-Jun-2020 465 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

Read More

C# Program to change a character from a string

karthikeya Boyini
karthikeya Boyini
Updated on 22-Jun-2020 342 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

Read More

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

Samual Sam
Samual Sam
Updated on 22-Jun-2020 161 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

Read More

Scope of Variables in C#

Samual Sam
Samual Sam
Updated on 22-Jun-2020 1K+ Views

The scope of a variable is a region of code that indicates where the variables are being accessed.For a variable, it has the following levels −Method LevelVariable declared inside a method is a local variable.Class LevelVariable declared inside a class is a local variable are class member variables.Let us see an example of scope of variables −Example Live Demousing System; namespace Demo {    class Program {       public int Divide(int num1, int num2) {          // local variable in a method          int result;          result = num1 ...

Read More

C# program to convert an array to an ordinary list with the same items

Samual Sam
Samual Sam
Updated on 22-Jun-2020 133 Views

Set an array −int[] arr = { 23, 66, 96, 110 };Now, create a new list −var list = new List();Use the Add method and add the array elements to the list −for (int i = 0; i < arr.Length; i++) {    list.Add(arr[i]); }The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       int[] arr = { 23, 66, 96, 110 };       var list = new List();       for (int i = 0; i < arr.Length; i++) {          list.Add(arr[i]);       }       foreach(int res in list) {          Console.WriteLine(res);       }    } }Output23 66 96 110

Read More

File Permissions in C#

George John
George John
Updated on 22-Jun-2020 3K+ Views

For File Permission in C#, use the FileIOPermission Class. It controls the ability to access files and folders.The following are the properties of File Permissions class −Sr.No.Methods & Description1AllFilesGets or sets the permitted access to all files.2AllLocalFilesGets or sets the permitted access to all local files.The following are the methods of File Permission class −Sr.No.Methods & Description1AddPathList(FileIOPermissionAccess, String)This method adds access for the specified file or directory to the existing state of the permission2Copy()This method creates and returns an identical copy of the current permission.3GetType()The GetType() method gets the type of the current instance.4ToXml()Creates an XML encoding of the permission ...

Read More

Pair Class in C#

Ankith Reddy
Ankith Reddy
Updated on 22-Jun-2020 6K+ Views

The Pair class is the KeyValuePair class that stores a pair of values in a single list with C#.Declare KeyValuePair −var myList = new Liststring, int>>(); Now, add some elements: myList.Add(new KeyValuePair("Laptop", 1)); myList.Add(new KeyValuePair("Desktop System", 2)); myList.Add(new KeyValuePair("Tablet", 3)); myList.Add(new KeyValuePair("Mobile", 4)); myList.Add(new KeyValuePair("E-Book Reader", 5)); myList.Add(new KeyValuePair("LED", 6));Display the KeyValuePair now as shown below −Example Live Demousing System; using System.Collections.Generic; class Program {    static void Main() {       var myList = new List();       // adding elements       myList.Add(new KeyValuePair ("Laptop", 1));       myList.Add(new KeyValuePair ("Desktop System", 2));   ...

Read More
Showing 731–740 of 1,951 articles
« Prev 1 72 73 74 75 76 196 Next »
Advertisements