Csharp Articles

Page 106 of 196

C# int.Parse Vs int.TryParse Method

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 6K+ 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 −Exampleusing 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.Exampleusing System.IO; using ...

Read More

C# Program to remove duplicate characters from String

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 11K+ 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 −Exampleusing 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

Read More

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

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ 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 −Exampleusing 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

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

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 428 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.Exampleusing 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

Read More

How to remove items from a list in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 509 Views

Firstly, set a list and add elements.List myList = new List(); myList.Add("Jennings"); myList.Add("James"); myList.Add("Chris");Let’s say you need to delete the element “James” now. For that, use the Remove() method.myList.Remove("James");Here is the complete code.Exampleusing System.Collections.Generic; using System; class Program {    static void Main() {       List myList = new List();       myList.Add("Jennings");       myList.Add("James");       myList.Add("Chris");       Console.WriteLine("Initial List...");       foreach(string str in myList) {          Console.WriteLine(str);       }       myList.Remove("James");       Console.WriteLine("New List...");       ...

Read More

How to print the contents of array horizontally using C#?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

Set an array.int[] array = new int[] { 50, 100, 150, 200, 250, 300, 350, 400 };Now, if you will print this array using a loop and new line, then the arrays will be visible vertically.To work it horizontally, use the Join() method and set spaces to separate array elements.string.Join(" ", array)Let us see the complete code.Exampleusing System; using System.Linq; using System.IO; class Program {    static void Main() {       int[] array = new int[] { 50, 100, 150, 200, 250, 300, 350, 400 };       Console.WriteLine(string.Join(" ", array));    } }Output50 100 150 200 250 300 350 400

Read More

Path methods in C#

Samual Sam
Samual Sam
Updated on 11-Mar-2026 313 Views

To handle File Paths in C#, use the Path methods. These methods come under System.IO Namespace.Some of them are −GetExtensionRetrieve the extension of the file using the GetExtension() method.For example, .txt, .dat, etc.GetFileNameRetrieve the name of the file using the GetFileName() method.For example, new.txt, details.dat, etc.GetFileNameWithoutExtensionRetrieve the name of the file without extension using the GetFileNameWithoutExtension() method.For example, new, details, etc.Let us see an example −Exampleusing System.IO; using System; class Program {    static void Main() {       string myPath = "D:\one.txt";       string fileExtension = Path.GetExtension(myPath);       string fileName = Path.GetFileName(myPath);     ...

Read More

C# Program to remove whitespaces in a string

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 9K+ Views

Let’s say the following is the string −StringBuilder str = new StringBuilder("Patience is key!");To remove whitespace, you can use the replace method.str.Replace(" ", "");Let us see the complete code.Exampleusing System; using System.Text; class Demo {    static void Main() {       // Initial String       StringBuilder str = new StringBuilder("Patience is key!");       Console.WriteLine(str.ToString());       // Replace       str.Replace(" ", "");       // New String       Console.WriteLine(str.ToString());       Console.ReadLine();    } }OutputPatience is key! Patienceiskey!

Read More

Represent Int32 as a Binary String in C#

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 10K+ Views

To represent Int632as a Binary string in C#, use the ToString() method and set the base as the ToString() method’s second parameter i.e. 2 for Binary.Int32 represents a 32-bit signed integer.Firstly, set an Int64 variable −int val = 30;Now, convert it to a binary string by including 2 as the second parameter.Convert.ToString(val, 2)Exampleusing System; class Demo {    static void Main() {       int val = 30;       Console.WriteLine("Integer: "+val);       Console.Write("Binary String: "+Convert.ToString(val, 2));    } }OutputInteger: 30 Binary String: 11110

Read More

C# Round-trip ("R") Format Specifier

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

This round-trip ("R") format specifier is supported for the Single, Double, and BigInteger types.It ensures that a numeric value converted to a string is parsed back into the same numeric value.Let us see an example −Firstly, we have a double variable.double doubleVal = 0.91234582637;Now, use the ToString() method: and set the Round-trip format specifier.doubleVal.ToString("R", CultureInfo.InvariantCulture);Let us see the complete example −Exampleusing System; using System.Numerics; using System.Globalization; class Demo {    static void Main() {       double doubleVal = 0.91234582637;       string str = doubleVal.ToString("R", CultureInfo.InvariantCulture);       double resRound = double.Parse(str, CultureInfo.InvariantCulture);       ...

Read More
Showing 1051–1060 of 1,951 articles
« Prev 1 104 105 106 107 108 196 Next »
Advertisements