Java URI getQuery()方法及示例
getQuery()函数是 URL类 的一部分 。 函数getQuery()返回指定URL的查询。
函数签名:
public String getQuery()
语法:
url.getQuery()
参数 :该函数不需要任何参数
返回类型: 该函数返回指定URL的字符串类型查询。
下面的程序说明了getQuery()函数的使用。
例1 :给定一个URL,我们将使用getQuery()函数获得查询。
// Java program to show the use// of the function getQuery() 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/url-getprotocol-method-in-java-with-examples?title=protocol"); // get the Query String _Query = url.getQuery(); // display the URL System.out.println("URL = " + url); // display the Query System.out.println(" Query=" + _Query); } // if any error occurs catch (Exception e) { // display the error System.out.println(e); } }}
输出:
URL = https:// www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol Query=title=protocol
例2 :现在我们将不提供任何查询,而是使用函数来获取查询并查看结果。
// Java program to show the use// of the function getQuery() 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/url-getprotocol-method-in-java-with-examples"); // get the Query String _Query = url.getQuery(); // display the URL System.out.println("URL = " + url); // display the Query System.out.println(" Query=" + _Query); } // if any error occurs catch (Exception e) { // display the error System.out.println(e); } }}输出:
URL = https:// www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples Query=null
