- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 35163 Articles for Programming
242 Views
The following is our string −string myStr = "5";To check whether the above is a string with numeric representation, use TryParse and out.int.TryParse(myStr, out a);Here is the complete code.Example Live Demousing System.IO; using System; class Program { static void Main() { bool res; int a; string myStr = "5"; // checking for valid string representation of a number res = int.TryParse(myStr, out a); Console.WriteLine(res); } }OutputTrue
2K+ Views
To validate, you need to check for the protocols.http httpsWith that, you need to check for .com, .in, .org, etc.For this, use the following regular expression −(http|http(s)?://)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?The following is the code −Example Live Demousing System; using System.Text.RegularExpressions; namespace RegExApplication { class Program { private static void showMatch(string text, string expr) { Console.WriteLine("The Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); foreach (Match m in mc) { Console.WriteLine(m); } } ... Read More
9K+ Views
Use Hashset to remove duplicate characters.Here is the string −string myStr = "kkllmmnnoo";Now, use HashSet to map the string to char. This will remove the duplicate characters from a string.var unique = new HashSet(myStr);Let us see the complete example −Example Live Demousing System; using System.Linq; using System.Collections.Generic; namespace Demo { class Program { static void Main(string[] args) { string myStr = "kkllmmnnoo"; Console.WriteLine("Initial String: "+myStr); var unique = new HashSet(myStr); Console.Write("New String after removing duplicates: "); foreach (char c in unique) Console.Write(c); } } }OutputInitial String: kkllmmnnoo New String after removing duplicates: klmno
3K+ Views
Convert a string representation of number to an integer, using the int.TryParse and intParse method in C#.If the string cannot be converted, then the int.TryParse method returns false i.e. a Boolean value, whereas int.Parse returns an exception.Let us see an example of int.Parse method −Example Live Demousing System.IO; using System; class Program { static void Main() { int res; string myStr = "120"; res = int.Parse(myStr); Console.WriteLine("String is a numeric representation: "+res); } }OutputString is a numeric representation: 120Let us see an example of int.TryParse method.Example Live Demousing ... Read More
5K+ Views
Set two arrays.var val = new [] { 20, 40, 60}; var str = new [] { "ele1", "ele2", "ele3"};Use the zip() method to process the two arrays in parallel.var res = val.Zip(str, (n, w) => new { Number = n, Word = w });The above fetches both the arrays with int and string elements respectively.Now, use foreach to iterate the two arrays −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { var val = new [] { 20, 40, 60}; var str = new ... Read More
203 Views
Firstly, set two lists in C#.List OneList list1 = new List (); list1.Add("P"); list1.Add("Q"); list1.Add("R"); list1.Add("S"); list1.Add("T"); list1.Add("U"); list1.Add("V"); list1.Add("W");List TwoList list2 = new List (); list2.Add("T"); list2.Add("U"); list2.Add("V"); list2.Add("W");Now, to get the different values in both the lists, use the Except method. It returns the values in the first list which is not present in the second list.Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List list1 = new List (); list1.Add("P"); list1.Add("Q"); ... Read More
620 Views
The Func generic type store anonymous methods and is a parameterized type.In the below example, we have 4 func type instance −The first type receives int and returns stringFunc one = (p) => string.Format("{0}", p);The second type receives bool & long and returns stringFunc two = (q, p) =>string.Format("{0} and {1}", q, p);The third type receives bool & int and returns stringFunc three = (q, p) => string.Format("{0} and {1}", q, p);The fourth type receives decimal and returns stringFunc four = (p) =>string.Format("{0}", p);Let us see how to display them −Example Live Demousing System; using System.IO; namespace Demo { ... Read More
216 Views
EnumerateFile() method is used in C# to get all the files. Use AllDirectories property to recurse through directories −Directory.EnumerateFiles(@"D:\NEW", "*.*", SearchOption.AllDirectories)To get the list of files in a directory, use the SearchOptions.AllDirectories in C# as shown above.Let us see how −Exampleusing System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { foreach (string allFiles is Directory.EnumerateFiles(@"D:\NEW","*.*",SearchOption.AllDirectories)) { Console.WriteLine(allFiles); } } } }OutputThe following is the output −D:\NEW\my.txt D:\NEW\amit.html D:\NEW\tutorials\java\a.java
152 Views
To get all the disk drives on a system, use the GetLogicalDrives() method in C# −Environment.GetLogicalDrives()Use it with Join method to get the comma-separated list of logical drives −string.Join(",", Environment.GetLogicalDrives())Example Live Demousing System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { Console.WriteLine(string.Join(",", Environment.GetLogicalDrives())); } } }Output/,/etc/resolv.conf,/etc/hostname,/etc/hosts,/run/secrets,/home/cg/root
To Continue Learning Please Login
Login with Google