C# Program to Search for a String in an Array of Strings

karthikeya Boyini
Updated on 23-Jun-2020 08:57:43

3K+ Views

Use Linq Contains() method to search for as specific string in an array of strings.string[] arr = { "Bag", "Pen", "Pencil"};Now, add the string in a string variable i.e. the string you want to search.string str = "Pen";Use the Contains() method to search the above string.arr.AsQueryable().Contains(str);Let us see the entire example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       string[] arr = { "Bag", "Pen", "Pencil"};       string str = "Pen";       bool res = arr.AsQueryable().Contains(str);       Console.WriteLine("String Pen is in the array? "+res);   ... Read More

C# LINQ Count Method

Arjun Thakur
Updated on 23-Jun-2020 08:57:19

684 Views

The Count method returns the count of elements in a sequence.Let us first set an array.string[] arr = { "Java", "C++", "Python"};Now, use the Count() method to count the array elements.arr.AsQueryable().Count();The following is the complete example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       string[] arr = { "Java", "C++", "Python"};       int arr_count = arr.AsQueryable().Count();       Console.WriteLine("Count of arrays: {0}", arr_count);    } }OutputCount of arrays: 3

C# DefaultIfEmpty Method

Samual Sam
Updated on 23-Jun-2020 08:56:58

295 Views

This method is used to handle empty collections. Instead of showing an error, this method displays a default value.We have the following list.List myList = new List();As you can see, since the above list is empty, we can display the default value.var res = myList.DefaultIfEmpty();Let us see an example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       List myList = new List();       var res = myList.DefaultIfEmpty();       foreach (var a in res) {          Console.WriteLine(a);       }    } }Output0

Get Upper Bound and Lower Bound of a Three-Dimensional Array in C#

Ankith Reddy
Updated on 23-Jun-2020 08:56:32

2K+ Views

To get the Upperbound and Lowerbound, use the GetUpperBound() GetLowerBound() methods in C#, respectively.The parameter to be set under these methods is the dimensions i.e.Let’s say our 3D array is −int[, , ] arr = new int[2, 3, 4];For a three-dimensional arrays, dimension 0.arr.GetUpperBound(0) arr.GetLowerBound(0)For a three-dimensional arrays, dimension 1.arr.GetUpperBound(1) arr.GetLowerBound(1)For a three-dimensional arrays, dimension 2.arr.GetUpperBound(2) arr.GetLowerBound(2)Example Live Demousing System; class Program {    static void Main() {       int[, , ] arr = new int[2, 3, 4];       Console.WriteLine("Dimension 0 Upper Bound: {0}", arr.GetUpperBound(0).ToString());       Console.WriteLine("Dimension 0 Lower Bound: {0}", arr.GetLowerBound(0).ToString());       ... Read More

Understanding Chash OverflowException

karthikeya Boyini
Updated on 23-Jun-2020 08:56:02

973 Views

OverflowException is thrown when the parameter value is out of integer ranges.Let us see an example.When we set a value to int.Parse() method that is out of integer range, then OverflowException is thrown as shown below −Example Live Demousing System; class Demo {    static void Main() {       string str = "757657657657657";       int res = int.Parse(str);    } }OutputThe following error is thrown when the above program is compiled since we have passed a value that is out of integer (Int32) range.Unhandled Exception: System.OverflowException: Value was either too large or too small for an Int32.

C# Decimal D Format Specifier

George John
Updated on 23-Jun-2020 08:55:14

2K+ Views

The "D" (or decimal) format specifier works for integer type. It converts a number to a string of decimal digits (0-9).Let’say the following is our number.int val = 467;Now to return the result as 0467, use the following decimal format specifier.val.ToString("D4")Let us see another example.Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       int val;       val = 877;       Console.WriteLine(val.ToString("D"));       Console.WriteLine(val.ToString("D4"));       Console.WriteLine(val.ToString("D8"));    } }Output877 0877 00000877

Get Value of Usemap Attribute in JavaScript

Sharon Christine
Updated on 23-Jun-2020 08:53:15

302 Views

To get the value of the usemap attribute of a link in JavaScript, use the useMap property. The usemap attribute is used to tell that the document is html (text/html) or css (text/css), etc.Client-side image maps are enabled by the usemap attribute for the tag and defined by special and extension tags. The image that is going to form the map is inserted into the page using the element as normal, except that it carries an extra attribute called usemap.ExampleYou can try to run the following code to get the value of the usemap attribute of a link ... Read More

Match Regular Expression Against a String

Nancy Den
Updated on 23-Jun-2020 08:51:16

337 Views

Use the match() method to match a regular expression against a string in JavaScript. You can try to run the following code to match a regular expression −The following is the parameter −match( param ) − A regular expression object.ExampleYou can try to run the following code to match a regular expression against a string −           JavaScript String match() Method                        var str = "For more information, see Chapter 3.4.5.1";          var re = /(chapter \d+(\.\d)*)/i;          var found = str.match( re );                    document.write(found );          

Represent Int64 as a String in C#

Samual Sam
Updated on 23-Jun-2020 08:50:51

293 Views

Int64 represents a 64-bit signed integer. To represent it as a string, use the ToString() method.Firstly, declare and initialize an Int64 variable.long val = 8766776;Now, represent it as a string.val.ToString()Let us see the complete example.Example Live Demousing System; class Demo {    static void Main() {       long val = 8766776;       Console.Write("Long converted to string = "+val.ToString());    } }OutputLong converted to string = 8766776

Represent int64 as a Hexadecimal String in C#

Chandu yadav
Updated on 23-Jun-2020 08:50:32

2K+ Views

To represent Int64 as a Binary string in C#, use the ToString() method and set the base as the ToString() method’s second parameter i.e.16 for Hexadecimal.Int64 represents a 64-bit signed integer.Firstly, set an Int64 variable.long val = 947645;Now, convert it to a hex string by including 16 as the second parameter.Convert.ToString(val, 16)ExampleLive Demousing System; class Demo {    static void Main() {       long val = 947645;       Console.WriteLine("Long: "+val);       Console.Write("Hex String: "+Convert.ToString(val, 16));    } }OutputLong: 947645 Hex String: e75bd

Advertisements