Uri.GetLeftPart() Method in C# with Examples

Uri.GetLeftPart() Method is an instance method that is used to get a specified part from the given URI based on passed UriPartial enum.

Syntax: string Uri.GetLeftPart (UriPartial Part);

Parameters:

  • Part: It represents UriPartial to get specified part from Uri.

Return Value: This method returns string value to represent specified part of Uri.

Exception: There are two type of exception are:

  • System.ArgumentException: If the specified part is not valid.
  • System.InvalidOperationException: If the current Uri instance is not an absolute instance.

Below programs illustrate the use of Uri.GetLeftPart() Method:



Example 1:

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the 
// Uri.GetLeftPart() Method 
using System; 
using System.Globalization; 
    
class GFG { 
    
     // Main Method 
    public static void Main() 
    
        // Declaring and initializing Uri 
        Uri  val;
          
        val = new Uri("https://www.geeksforgeeks.org/data-structure");
          
        // Use of Uri.GetLeftPart() Method
        // Getting the Authority
        string str = val.GetLeftPart(UriPartial.Authority);
          
        Console.WriteLine(str); 
    
}

chevron_right


Output:

https://www.geeksforgeeks.org

Example 2:

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the 
// Uri.GetLeftPart() Method 
using System; 
using System.Globalization; 
    
class GFG { 
    
     // Main Method 
    public static void Main() 
    
        // Declaring and initializing Uri 
        Uri  val;
          
        val = new Uri("https://www.geeksforgeeks.org/");
          
        // Use of Uri.GetLeftPart() Method
        // Getting the Path
        string str1 = val.GetLeftPart(UriPartial.Path);
        Console.WriteLine(str1); 
          
        // Getting the Query
        string str2 = val.GetLeftPart(UriPartial.Query);
        Console.WriteLine(str2);
          
        // Getting the Scheme
        string str3 = val.GetLeftPart(UriPartial.Scheme);
        Console.WriteLine(str3);
    
}

chevron_right


Output:

https://www.geeksforgeeks.org/
https://www.geeksforgeeks.org/
https://



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.