Get the anchor value of the given URL in Java

Description

The following code shows how to get the anchor value of the given URL.

Example


public class Main{
//from w ww  .  ja va2 s. c  o m
  public static final String ANCHOR_CHAR = "#"; //$NON-NLS-1$

  public static String getAnchor(String url) {
    if (url != null) {
      int anchorPosition = url.indexOf(ANCHOR_CHAR);
      if (anchorPosition >= 0) {
        return url.substring(anchorPosition + 1);
      }
    }
    return null;
  }
  public static void main(String[] argv){
    String url = "http://java2s.com/nonexist#test";
    System.out.println(getAnchor(url));
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode