Java URI getRef()方法及示例
getRef()函数是URL类的一个组成部分。getRef()函数返回一个指定的URL的引用或锚点部分。
函数签名:
public String getRef()
语法:
url.getRef()
参数: 这个函数不需要任何参数
返回类型: 该函数返回字符串类型
下面的程序说明了getRef()函数的使用。
例1 :给定一个URL,我们将使用getRef()函数获得参考或锚点。
// Java program to show the// use of the function getRef() 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 Reference or anchor String _Ref=url.getRef(); // display the URL System.out.println("URL = "+url); // display the Reference or anchor System.out.println(" Reference or anchor="+_Ref); } // if any error occurs catch (Exception e) { // display the error System.out.println(e); } }}
输出:
URL = https:// www.geeksforgeeks.org#Arnab_Kundu Reference or anchor=Arnab_Kundu
例2 :现在我们将不提供任何锚点,并使用该函数来获取引用或锚点,看看结果。
// Java program to show the// use of the function getRef() 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 Reference or anchor String _Ref = url.getRef(); // display the URL System.out.println("URL = " + url); // display the Reference or anchor System.out.println(" Reference or anchor= " + _Ref); } // if any error occurs catch (Exception e) { // display the error System.out.println(e); } }}输出:
URL = https:// www.geeksforgeeks.org Reference or anchor= null
