Found 35163 Articles for Programming

Environment.NewLine in C#

Arjun Thakur
Updated on 22-Jun-2020 15:02:36

1K+ Views

The Enviornment.NewLine in C# is used to add newline.To set a new line in between words −str = "This is demo text!" + Environment.NewLine + "This is demo text on next line!";The following is the code −Example Live Demousing System; using System.IO; namespace Demo {    class Program {       static void Main(string[] args) {          string str = "This is demo text!" + Environment.NewLine + "This is demo text on next line!";          Console.Write(str);       }    } }OutputThis is demo text! This is demo text on next line!

C# program to get the file name in C#

Chandu yadav
Updated on 22-Jun-2020 15:03:07

534 Views

Set the path name in a string −string myPath = "D:ew\quiz.txt";Now, use the GetFileName() method to get the name of the file −Path.GetFileName(myPath)The following is the complete code −Example Live Demousing System; using System.IO; namespace Demo {    class Program {       static void Main(string[] args) {          string myPath = "D:ew\quiz.txt";          // get extension          Console.WriteLine("Extension: "+Path.GetExtension(myPath));          // get path          Console.WriteLine("File Path: "+Path.GetFileName(myPath));       }    } }OutputExtension: .txt File Path: D:ew\quiz.txt

C# program to get the extension of a file in C#

Arjun Thakur
Updated on 22-Jun-2020 15:03:30

212 Views

To handle file paths, the Path class is used in C#.Set the file name in a string −string myPath = "D:ew\quiz.txt";Now, to get the extension, use the GetExtension() method −Path.GetExtension(myPath)Here is the complete code −Example Live Demousing System; using System.IO; namespace Demo {    class Program {       static void Main(string[] args) {          string myPath = "D:ew\quiz.txt";          Console.WriteLine(Path.GetExtension(myPath));       }    } }Output.txt

C# program to get the total number of cores on a computer

Ankith Reddy
Updated on 22-Jun-2020 15:03:52

139 Views

Use the Environment.ProcessorCount to get the total number of cores on a computer −Environment.ProcessorCountThe following is the code that displays the total number of cores on a computer in C# −ExampleUsing System; namespace Demo {    class Program {       static void Main(string[] args) {          Console.WriteLine(Environment.ProcessorCount);       }    } }

Replace parts of a string with C# Regex

Arjun Thakur
Updated on 22-Jun-2020 14:52:47

1K+ Views

Set a string −string str = "Bit and Bat";Let’s say you need to replace whatever comes inside B and t to A and capitalize the complete string. For that, use Replace −Regex.Replace(str, "B.t", "BAT");Let us see the complete code −Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo {    class Program {       static void Main(string[] args) {          string str = "Bit and Bat";          Console.WriteLine(str);          string res = Regex.Replace(str, "B.t", "BAT");          Console.WriteLine(res);       }    } }OutputBit and Bat BAT and BAT

C# Program to remove the end part of a string

Chandu yadav
Updated on 22-Jun-2020 14:53:18

414 Views

Use the Regex.Replace method to remove the end part of a string in C#.The following is the string −string s1 = "Demo Text!";Now, let us say you need to remove the exclamation mark (!) from the string. For that just set it to empty using replace −System.Text.RegularExpressions.Regex.Replace(s1, "!", "");Here is the complete code −Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo {    class Program {       static void Main(string[] args) {          string s1 = "Demo Text!";          // replace the end part          string s2 = System.Text.RegularExpressions.Regex.Replace(s1, "!", ... Read More

C# Program to match all the digits in a string

George John
Updated on 22-Jun-2020 14:53:57

299 Views

To match all digits in a string, use C# Regex.Firstly, set a string with digits −string str = "These are my marks: 90 out of 100!";Use the following regular expression to get digits in a string −@"\d+"The following is the code −Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo {    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 generate random lowercase letter

Ankith Reddy
Updated on 22-Jun-2020 14:54:23

1K+ Views

Firstly, set Random class −Random random = new Random();Set a range under the Next() method. This displays a letter between 0 and 26.int a = random.Next(0, 26);Here is the complete code −Example Live Demousing System; using System.IO; using System.Linq; class Demo {    static void Main() {       Random random = new Random();       // random lowercase letter       int a = random.Next(0, 26);       char ch = (char)('a' + a);       Console.WriteLine(ch);    } }Outputt

Randomize string in C#

Arjun Thakur
Updated on 22-Jun-2020 14:54:50

1K+ Views

To randomize string, firstly use Random class −Random r = new Random();Now, use the Next() method with OrderBy() −string random = new string(str.ToCharArray().OrderBy(s => (r.Next(2) % 2) == 0).ToArray());Here is the compete code that displays randomize string −Example Live Demousing System; using System.IO; using System.Linq; class Demo {    static void Main() {       const string str = "electronics";       Random r = new Random();       string random = new string(str.ToCharArray().OrderBy(s => (r.Next(2) % 2) == 0).ToArray());       Console.WriteLine("String = {0}", str);       Console.WriteLine("Random String = {0}",random);       Console.Read();    } }OutputString = electronics Random String = lericsecton

C# Program to display temporary file names

karthikeya Boyini
Updated on 22-Jun-2020 14:55:40

141 Views

The GetTempPath() method in C# displays temporary file names −Path.GetTempPath();Get the names in a variable and display −string tempFile = Path.GetTempPath();The following is the code −Example Live Demousing System; using System.IO; class Demo {    static void Main() {       string tempFile = Path.GetTempPath();       Console.WriteLine(tempFile);    } }Output/tmp/

Advertisements