當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java URL toExternalForm()用法及代碼示例


toExternalForm()函數是URL類的一部分。函數toExternalForm()返回指定URL的字符串表示形式。通過調用此對象的流協議處理程序的toExternalForm()函數來創建字符串。

函數簽名:

public String toExternalForm()

用法:


url.toExternalForm()

參數:此函數不需要任何參數
返回類型:函數返回字符串類型

以下示例程序旨在說明toExternalForm()函數的用法:

例子1:給定一個URL,我們將以URL的字符串表示形式

// Java program to show the 
// use of the function toExternalForm() 
  
import java.net.*; 
  
class Solution { 
    public static void main(String args[]) 
    { 
        // url  object 
        URL url = null; 
  
        try { 
            // create a URL 
            url = new URL("https:// www.geeksforgeeks.org"); 
  
            // get the  ExternalForm 
            String _ExternalForm = url.toExternalForm(); 
  
            // display the URL 
            System.out.println("URL = " + url); 
  
            // display the  ExternalForm 
            System.out.println(" String representation= "
                               + _ExternalForm); 
        } 
        // if any error occurs 
        catch (Exception e) { 
  
            // display the error 
            System.out.println(e); 
        } 
    } 
}
輸出:
URL = https:// www.geeksforgeeks.org
 String representation= https:// www.geeksforgeeks.org

範例2:

// Java program to show the 
// use of the function toExternalForm() 
  
import java.net.*; 
  
class Solution { 
    public static void main(String args[]) 
    { 
        // url  object 
        URL url = null; 
  
        try { 
  
            // create a URL 
            url= new URL("https:// www.geeksforgeeks.org#Arnab_Kundu"); 
              
            // get the  ExternalForm 
            String _ExternalForm = url.toExternalForm(); 
  
            // display the URL 
            System.out.println("URL = " + url); 
  
            // display the  ExternalForm 
            System.out.println(" String representation= "
                               + _ExternalForm); 
        } 
  
        // if any error occurs 
        catch (Exception e) { 
  
            // display the error 
            System.out.println(e); 
        } 
    } 
}
輸出:
URL = https:// www.geeksforgeeks.org#Arnab_Kundu
 String representation= https:// www.geeksforgeeks.org#Arnab_Kundu


相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 URL toExternalForm() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。