当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。