Convert URI to Strings in C#
In C#, the Uri
class is used to represent URIs. It provides methods to manipulate and retrieve various components of a URI. However, there are scenarios where you might need to convert a URI to a string, for instance, when you want to display it, store it, or pass it as a parameter in a method. The ToString()
method of the Uri
class is the simplest way to achieve this.
using System;
class Program
{
static void Main()
{
// Sample URI
Uri sampleUri = new Uri("https://www.example.com/path/page.html?query=123");
// Convert URI to string
string uriString = sampleUri.ToString();
// Display the original URI and the converted string
Console.WriteLine("Original URI: " + sampleUri);
Console.WriteLine("Converted String: " + uriString);
}
}
Output:
Original URI: https://www.example.com/path/page.html?query=123
Converted String: https://www.example.com/path/page.html?query=123
In this example, we create a Uri
object representing a sample URI. The ToString()
method is then used to convert this URI to a string. The program outputs both the original URI and the converted string.
Comments (0)