- 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
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!
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
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
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); } } }
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
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
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
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
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
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/
To Continue Learning Please Login
Login with Google