Android HTML Decode getHrefInnerHtml(String href)

Here you can find the source of getHrefInnerHtml(String href)

Description

get innerHtml from href
 getHrefInnerHtml(null)                                  = "" getHrefInnerHtml("")                                    = "" getHrefInnerHtml("mp3")                                 = "mp3"; getHrefInnerHtml("<a innerHtml</a>")                    = "<a innerHtml</a>"; getHrefInnerHtml("<a>innerHtml</a>")                    = "innerHtml"; getHrefInnerHtml("<a<a>innerHtml</a>")                    = "innerHtml"; getHrefInnerHtml("<a href="baidu.com">innerHtml</a>")               = "innerHtml"; getHrefInnerHtml("<a href="baidu.com" title="baidu">innerHtml</a>") = "innerHtml"; getHrefInnerHtml("   <a>innerHtml</a>  ")                           = "innerHtml"; getHrefInnerHtml("<a>innerHtml</a></a>")                      = "innerHtml"; getHrefInnerHtml("jack<a>innerHtml</a></a>")                  = "innerHtml"; getHrefInnerHtml("<a>innerHtml1</a><a>innerHtml2</a>")        = "innerHtml2"; 

Parameter

Parameter Description
href a parameter

Return

  • if href is null, return ""
  • if not match regx, return source
  • return the last string that match regx

Declaration

public static String getHrefInnerHtml(String href) 

Method Source Code

//package com.java2s;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**//  w  ww.j  av  a2s  .c  om
     * get innerHtml from href
     * 
     * <pre>
     * getHrefInnerHtml(null)                                  = ""
     * getHrefInnerHtml("")                                    = ""
     * getHrefInnerHtml("mp3")                                 = "mp3";
     * getHrefInnerHtml("&lt;a innerHtml&lt;/a&gt;")                    = "&lt;a innerHtml&lt;/a&gt;";
     * getHrefInnerHtml("&lt;a&gt;innerHtml&lt;/a&gt;")                    = "innerHtml";
     * getHrefInnerHtml("&lt;a&lt;a&gt;innerHtml&lt;/a&gt;")                    = "innerHtml";
     * getHrefInnerHtml("&lt;a href="baidu.com"&gt;innerHtml&lt;/a&gt;")               = "innerHtml";
     * getHrefInnerHtml("&lt;a href="baidu.com" title="baidu"&gt;innerHtml&lt;/a&gt;") = "innerHtml";
     * getHrefInnerHtml("   &lt;a&gt;innerHtml&lt;/a&gt;  ")                           = "innerHtml";
     * getHrefInnerHtml("&lt;a&gt;innerHtml&lt;/a&gt;&lt;/a&gt;")                      = "innerHtml";
     * getHrefInnerHtml("jack&lt;a&gt;innerHtml&lt;/a&gt;&lt;/a&gt;")                  = "innerHtml";
     * getHrefInnerHtml("&lt;a&gt;innerHtml1&lt;/a&gt;&lt;a&gt;innerHtml2&lt;/a&gt;")        = "innerHtml2";
     * </pre>
     * 
     * @param href
     * @return <ul>
     *         <li>if href is null, return ""</li>
     *         <li>if not match regx, return source</li>
     *         <li>return the last string that match regx</li>
     *         </ul>
     */
    public static String getHrefInnerHtml(String href) {
        if (isEmpty(href)) {
            return "";
        }

        String hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*";
        Pattern hrefPattern = Pattern.compile(hrefReg,
                Pattern.CASE_INSENSITIVE);
        Matcher hrefMatcher = hrefPattern.matcher(href);
        if (hrefMatcher.matches()) {
            return hrefMatcher.group(1);
        }
        return href;
    }

    /**
     * is null or its length is 0
     * 
     * <pre>
     * isEmpty(null) = true;
     * isEmpty(&quot;&quot;) = true;
     * isEmpty(&quot;  &quot;) = false;
     * </pre>
     * 
     * @param str
     * @return if string is null or its size is 0, return true, else return false.
     */
    public static boolean isEmpty(String str) {
        return (str == null || str.length() == 0);
    }
}

Related

  1. htmldecode(String str)
  2. fromHtml(String str)