Found 35163 Articles for Programming

How to validate a string for a numeric representation using TryParse in C#

karthikeya Boyini
Updated on 22-Jun-2020 15:14:57

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

How to validate a URL using regular expression in C#?

Samual Sam
Updated on 22-Jun-2020 15:15:44

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

C# Program to remove duplicate characters from String

karthikeya Boyini
Updated on 22-Jun-2020 15:16:43

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

C# int.Parse Vs int.TryParse Method

Chandu yadav
Updated on 22-Jun-2020 15:38:27

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

C# program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer

Samual Sam
Updated on 22-Jun-2020 15:17:23

205 Views

To fetch the consecutive 1’s, use the Bitwise Left Shift Operator. Here is our decimal number.i = (i & (i

How to iterate two Lists or Arrays with one foreach statement in C#?

karthikeya Boyini
Updated on 22-Jun-2020 15:18:04

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

C# program to find additional values in two lists

Samual Sam
Updated on 22-Jun-2020 15:20:42

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

Func generic type in C#

Ankith Reddy
Updated on 22-Jun-2020 15:00:01

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

EnumerateFiles method in C#

George John
Updated on 22-Jun-2020 15:01:48

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

GetLogicalDrives in C#

Chandu yadav
Updated on 22-Jun-2020 15:02:11

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

Advertisements