Found 34494 Articles for Programming

Bool Array in C#

George John
Updated on 22-Jun-2020 13:49:25

7K+ Views

In a bool array, you can store true and false values. To set a bool array, use the new operator −bool[] arr = new bool[5];To add elements in the array −arr[0] = true; arr[1] = true; arr[2] = false; arr[3] = true; arr[4] = true;Let us see the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       bool[] arr = new bool[5];       arr[0] = true;       arr[1] = true;       arr[2] = false;       arr[3] = true;       arr[4] = true;       Console.WriteLine("Displaying values...");       foreach (bool res in arr) {          Console.WriteLine(res);       }    } }OutputDisplaying values... True True False True True

C# program to separate joined strings in C#

Chandu yadav
Updated on 22-Jun-2020 13:49:53

41 Views

The following is the string array −string[] str = { "Java", "AngularJS", "Python", "jQuery", "HTML5" };Firstly, join it −string.Join(" ", str);Now to separate the above joined strings, use the Split() method as shown in the following code −Example Live Demousing System; public class Demo {    public static void Main() {       string[] str = { "Java", "AngularJS", "Python", "jQuery", "HTML5" };       // join words       string res = string.Join(" ", str);       Console.WriteLine("Joined Strings... "+res);       string[] convert = res.Split(' ');       Console.WriteLine("Separate Joined Strings..."); ... Read More

C# program to join words into a string in C#

Arjun Thakur
Updated on 22-Jun-2020 13:50:17

182 Views

Declare a string and add elements −string[] str = { "One", "Two", "Three", "Four", "Five" };Use the Join() method to join the words−string res = string.Join(" ", str);Let us see the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       string[] str = { "One", "Two", "Three", "Four", "Five" };       // join words       string res = string.Join(" ", str);       Console.WriteLine(res);    } }OutputOne Two Three Four Five

C# program to find the index of a word in a string

Ankith Reddy
Updated on 22-Jun-2020 13:50:59

472 Views

Declare and initialize an array −string[] str = new string[] {    "Cat",    "Mat",    "Rat" };Now, ue IndexOf() method to find the index of the word “Mat” −Array.IndexOf(str, "Mat");The following is the code −Example Live Demousing System; public class Demo {    public static void Main() {       string[] str = new string[] {          "Cat",          "Mat",          "Rat"       };       Console.WriteLine("Our Array =");       for (int i = 0; i < str.Length; i++) {          string res = str[i];          Console.WriteLine(res);       }       int findIndex = Array.IndexOf(str, "Mat");       Console.Write("Element Mat found at the following index: ");       Console.WriteLine(findIndex);    } }OutputOur Array = Cat Mat Rat Element Mat found at the following index: 1

C# program to create an empty string array

George John
Updated on 22-Jun-2020 13:51:23

3K+ Views

To create an empty string array −string[] str = new string[] {};Above, we haven’t added elements to the array, since it is empty.Even if we will loop though the array, it won’t display anything as shown below −Example Live Demousing System; public class Demo {    public static void Main() {       string[] str = new string[] {};       Console.WriteLine("String Array elements won't get displayed since it's empty...");       for (int i = 0; i < str.Length; i++) {          string res = str[i];          Console.WriteLine(res);       }    } }OutputString Array elements won't get displayed since it's empty...

C# program to iterate over a string array with for loop

Chandu yadav
Updated on 22-Jun-2020 13:52:06

2K+ Views

Create a string array −string[] str = new string[] {    "Videos",    "Tutorials",    "Tools",    "InterviewQA" };Loop until the length of the array −for (int i = 0; i < str.Length; i++) {    string res = str[i];    Console.WriteLine(res); }Here is the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       string[] str = new string[] {          "Videos",          "Tutorials",          "Tools",          "InterviewQA"       };           Console.WriteLine("String Array...");       for (int i = 0; i < str.Length; i++) {          string res = str[i];          Console.WriteLine(res);       }    } }OutputString Array... Videos Tutorials Tools InterviewQA

C# program to get the last element from an array

karthikeya Boyini
Updated on 22-Jun-2020 13:40:53

3K+ Views

Firstly, set an array −string[] str = new string[]{    "Java",    "HTML",    "jQuery",    "JavaScript",    "Bootstrap" };To get the value of the last element, get the length and display the following value −str[str.Length - 1]The above returns the last element.Here is the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       string[] str = new string[] {          "Java",          "HTML",          "jQuery",          "JavaScript",          "Bootstrap"       };       Console.WriteLine("Array...");       foreach(string res in str) {          Console.WriteLine(res);       }       Console.WriteLine("Last element: "+str[str.Length - 1]);    } }OutputArray... Java HTML jQuery JavaScript Bootstrap Last element: Bootstrap

C# program to return an array from methods

karthikeya Boyini
Updated on 22-Jun-2020 13:41:19

9K+ Views

Call a method under Join method to join words −string.Join(" ", display())Now set a string array −string[] str = new string[5];Add individual elements −str[0] = "My"; str[1] = "name"; str[2] = "is"; str[3] = "Brad"; str[4] = "Pitt";And return the same string array from method −return str;The following is the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       Console.WriteLine(string.Join(" ", display()));    }    static string[] display() {       string[] str = new string[5];       // string array elements       str[0] = "My";       str[1] = "name";       str[2] = "is";       str[3] = "Brad";       str[4] = "Pitt";       return str;    } }OutputMy name is Brad Pitt

C# Program to read contents of a file into a string at once

Samual Sam
Updated on 03-Apr-2020 10:36:03

217 Views

Use ReadToEnd() method to read the contents of a file in a string.Set it under StreamReader and read the file −using (StreamReader sr = new StreamReader("new.txt")){    string res = sr.ReadToEnd();    Console.WriteLine(res); }The following is the complete code −Example Live Demousing System.IO; using System; public class Demo {    public static void Main() {       using (StreamWriter sw = new StreamWriter("new.txt")) {          sw.WriteLine("One");          sw.WriteLine("Two");       }       using (StreamReader sr = new StreamReader("new.txt")) {          string res = sr.ReadToEnd();       ... Read More

Read in a file in C# with StreamReader

karthikeya Boyini
Updated on 03-Apr-2020 10:43:20

428 Views

To read text files, use StreamReader class in C#.Add the name of the file you want to read −StreamReader sr = new StreamReader("hello.txt");Use the ReadLine() method and get the content of the file in a string −using (StreamReader sr = new StreamReader("hello.txt")) {    str = sr.ReadLine(); } Console.WriteLine(str);Let us see the following code −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       string str;       using (StreamWriter sw = new StreamWriter("hello.txt")) {          sw.WriteLine("Hello");          sw.WriteLine("World");       }   ... Read More

Advertisements