The method returns a single specific element of a sequence. If the element is not present in the sequence, then the default value is returned.We have two string arrays here.string[] str1 = { "one" }; string[] str2 = { };First array is checked for a single element, whereas the second array is empty and checked using SingleorDefault.str2.AsQueryable().SingleOrDefault();The following is an example showing the usage of SingleorDefault() method.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { string[] str1 = { "one" }; string[] str2 = { }; ... Read More
To match any string containing zero or one p’s with JavaScript RegExp, use the p? Quantifier.ExampleYou can try to run the following code to match any string containing zero or one p. Here p is considered a number of occurrences − JavaScript Regular Expression var myStr = "Welcome 1, 100, 10000, 10000"; var reg = /10?/g; var match = myStr.match(reg); document.write(match);
Skip elements and return the remaining elements using the Skip() method.The following is an array.int[] marks = { 80, 55, 79, 99 };Now, let us skip 2 elements using Lambda Expressions, but this is done after arranging the elements in descending order.IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).Skip(2);Exampleusing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { int[] marks = { 80, 55, 79, 99 }; IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).Skip(2); Console.WriteLine("Skipped the result of 1st two students..."); foreach (int res in selMarks) { console.WriteLine(res); } } }
Let’s say we want to remove whitespace from the following string str1.string str1 = "Brad Pitt";Now, use Regex Replace to replace whitespace with empty. Here, we have used System.Text.RegularExpressions.string str2 = System.Text.RegularExpressions.Regex.Replace(str1, @"\s+", "");Let us see the complete example.Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo { class Program { static void Main(string[] args) { string str1 = "Brad Pitt"; Console.WriteLine(str1); string str2 = System.Text.RegularExpressions.Regex.Replace(str1, @"\s+", ""); Console.WriteLine(str2); } } }OutputBrad Pitt BradPitt
The numeric ("N") format specifier converts a number to a string of the following form −"-d,ddd,ddd.ddd…"Above,"-" is a negative number symbol if required,"d" is a digit (0-9), "," indicates a group separator,"." is a decimal point symbolExample Live Demousing System; using System.Globalization; class Demo { static void Main() { double val1 = -5566.789; Console.WriteLine(val1.ToString("N", CultureInfo.InvariantCulture)); int val2 = 87987766; Console.WriteLine(val2.ToString("N3", CultureInfo.InvariantCulture)); } }Output-5,566.79 87,987,766.000
The following is our list with elements −IList emp = new List() { new Employee() { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 } , new Employee() { EmployeeRank = 05, EmpName = "Raman", EmpMarks = 95 } };Now use orderby and descending to sort elements in descending order.var res = from str in emp orderby str.EmpName descending select str;Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { IList emp = new List() { new Employee() { EmployeeRank = 4, EmpName ... Read More
To represent Int32 as a Octal string in C#, use the ToString() method and set the base as the ToString() method’s second parameter i.e. 8 for Octal.Int32 represents a 32-bit signed integer.Firstly, set an Int32 variable.int val = 99;Now, convert it to octal string by including 8 as the second parameter.Convert.ToString(val, 8)Example Live Demousing System; class Demo { static void Main() { int val = 99; Console.WriteLine("Integer: "+val); Console.Write("Octal String: "+Convert.ToString(val, 8)); } }OutputInteger: 99 Octal String: 143
Use Orderby descending in C# to sort elements in descending order.The following is our list −IList emp = new List() { new Employee() { EmployeeRank = 3, EmpName = "Tom", EmpMarks = 90 } , new Employee() { EmployeeRank = 4, EmpName = "Katie", EmpMarks = 95 } };Now to sort the list according to Student name, use Orderby. The default is ascending, therefore for descending order, use descending.var res = from str in emp orderby str.EmpName descending select str;Here is the complete code.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { ... Read More
UShort represents a 16-bit unsigned integer.To implicitly convert of a 16-bit unsigned integer to a decimal, firstly set a ushort value.ushort val = 193;To convert ushort to decimal, assign the value.decimal dec; dec = val;Let us see an example.Exampleusing System; public class Demo { public static void Main() { ushort val = 2567; decimal dec; Console.WriteLine("Implicit conversion from 16-bit unsigned integer to Decimal"); dec = val; Console.WriteLine("Decimal = "+dec); } }
The Full Date Long Time standard format specifier’s custom date and time format string is defined by the current DateTimeFormatInfo.FullDateTimePattern property.The custom format string is −dddd, dd MMMM yyyy HH:mm:ssExample Live Demousing System; using System.Globalization; class Demo { static void Main() { DateTime myDate = new DateTime(2018, 8, 13, 9, 15, 0); Console.WriteLine(myDate.ToString("F", CultureInfo.CreateSpecificCulture("en-US"))); } }OutputMonday, August 13, 2018 9:15:00 AM
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP