Java去除字符串中结尾的所有br标签

/**
     * 剔除结尾的br-正则
     * @param cs 字符序列
     * @return 删除html标签后的字符序列
     */
    public static String replaceEndBrHtml(String cs){
        String rex = "^(.*)(
)$"; Pattern comPile = Pattern.compile(rex); Matcher matcher = comPile.matcher(cs); while(matcher.find()){ cs = matcher.group(1); matcher = comPile.matcher(cs); } return cs; } /** * 删除结尾的br * @param cs 字符序列 * @return 删除html标签后的字符序列 */ public static String deleteEndBrHtml(String cs){ if(isEmpty(cs)){ return ""; } String s2 = cs.replaceAll("
", "~").replaceAll("
", "~"); if(s2.lastIndexOf("\"")>0){ s2 = s2.substring(0,s2.length()-1); } int len = s2.length(); int st = 0; char[] val = s2.toCharArray(); while ((st < len) && (val[len - 1] == '~')) { len--; } String s3 = ((st > 0) || (len < s2.length())) ? s2.substring(st, len) : s2; String s4 = s3.replaceAll("~", "
"); return s4; } /** * 删除所有br * @param cs 字符序列 * @return 删除html标签后的字符序列 */ public static String deleteNotBrHtml(CharSequence cs){ if(isEmpty(cs)){ return ""; } return Pattern.compile("]*)>").matcher(cs).replaceAll("").replaceAll("
",""); }

你可能感兴趣的:(Java基础)