To replace a string in C#/CSharp, we make use Replace method. Example is given below. using System; using System.IO; namespace Hello_World { class Program { static void Main(string args) { string hello = "Hello World"; string myString = hello.Replace("World", "FWait"); Console.WriteLine(myString); } } }
To reverse a string in C#/CSharp, we first convert string to an array of characters. Then, use static method Reverse which resides in Array class to reverse the string. Example is given below. using System; using System.IO; namespace Hello_World { class Program { static void Main(string args) { string hello = "Hello World"; char array
To check if file exists in C#/CSharp, we make use of static function Exists of File class. This will return a boolean value. Example is given below. using System; using System.IO; namespace Hello_World { class Program { static void Main(string args) { string path = @"C:\file.txt"; if(File.Exists(path)) { Console.WriteLine("File Exists: Yes!"); } else { Console.WriteLine("File
To get file name from path in C#/CSharp, we make use of GetFileName() static method of Path class, which resides in System.IO namespace. Example is given below. using System; using System.IO; namespace Hello_World { class Program { static void Main(string args) { string file = @"C:\Users\ADMIN\Desktop\delete.txt"; string name = Path.GetFileName(file); Console.WriteLine(name); } } }
To get file extension in C#, we make use of GetExtension() static method of Path class, which resides in System.IO namespace. Example is given below. using System; using System.IO; namespace Hello_World { class Program { static void Main(string args) { string file = @"C:\Users\ADMIN\Desktop\delete.txt"; string extension = Path.GetExtension(file); Console.WriteLine(extension); } } }