⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringfilter.java

📁 是一个java写的 可以输入要下的歌名
💻 JAVA
字号:
/* * 作者: 胡李青 * qq: 31703299 * Copyright (c) 2007 huliqing * 主页 http://www.tbuy.biz/ * 你可以免费使用该软件,未经许可请勿作用于任何商业目的 */package mymp3;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * @author huliqing * @version 1.1 * <br/>最后修改:2007-12-02 * <p><b>qq:</b>31703299 * <p><b>E-mail:</b><a href="mailto:huliqing.cn@gmail.com">huliqing.cn@gmail.com</a> * <p><b>Homepage:</b><a href="http://www.tbuy.biz/">http://www.tbuy.biz/</a> */public class StringFilter {        // ------------------------------------------------------------filter    private static String[][] _chars={        {"&",   "&amp;"},        {"\n",  "<br>"},        {"<",   "&lt;"},        {">",   "&gt;"},        {" ",   "&nbsp;"},        {"\"",  "&quot;"}    };    /**     * 过滤器,将相应的字符转换为html代码,转换包括以下字符<br>     * & \n < > \ 及空格     * @param str     * @return newStr     */    public static String filter(String str) {        for (int i = 0; i < _chars.length; i++) {            str = str.replaceAll(_chars[i][0], _chars[i][1]);        }        return str;    }        /**     * 去除字符串中的html换行符: p br,     * @param source 原始字符     * @return newStr 清除后的字符     */    public static String clearBR(String source) {        //String regx = "</?(br|p)(\\s)*/?>";        String regx = "(</?(br|p)(\\s)*/?>)|(<tbody>?)";        Pattern p = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);        Matcher m = p.matcher(source);        StringBuffer sb = new StringBuffer();        while (m.find()) {            m.appendReplacement(sb, "");        }        m.appendTail(sb);        return sb.toString();    }        /**     * 从一个字串source中查找find指定的字符串,并返回匹配第一个find所指定的字符串的     * 起始位置(不分大小),该起始位置包含起始字符位置及结速字符位置,获取数据方法<br>     * fin[0] -> find所指定的字符的起始位置,没匹配则返回 -1,<br>     * fin[1] -> find所指定的字符的结速位置,没匹配则返回 -1,<br>     * @param source 源字符串     * @param find 所要查找的字符     * @return fin find所指定的字符在source中的起始位置数组     */    public static int[] findString(String source, String find) {        int[] fin = new int[]{-1, -1};        Pattern p = Pattern.compile(find, Pattern.CASE_INSENSITIVE);        Matcher m = p.matcher(source);        if (m.find()) {            fin[0] = m.start();            fin[1] = m.end();        }        return fin;    }        /**     * 在一个字符串source中搜索指定的字串find,并使用默认颜色设置前景色及背     * 景色.以加亮效果,需要使用html格式才能显示出效果,主要用于匹配查询,     * 该方法不区分大小写     * @see #IgnoreCaseSearch(String, String, String, String)     * @param source 原始字符串     * @param find 要查找的字符     * @return newStr 加了样式的新的字符串     */    public static String IgnoreCaseSearch(String source, String find) {        return IgnoreCaseSearch(source, find, null, null);    }        /**     * 在一个字符串source中搜索指定的字串find,并使用color设置前景色及bgcolor设置背     * 景色.需要使用html格式才能显示出效果,主要用于匹配查询,该方法不区分大小写     * @param source 原始字符串     * @param find 要查找的字符     * @param color 找到后改变其前景色:格式如 red,blue,或 #FFFFFF 或null     * @param bgcolor 找到后字串的背景色:格式如 red,blue,或 #FFFFFF 或null     * @return newStr 加了样式的新的字符串     */    public static String IgnoreCaseSearch(String source, String find, String color, String bgcolor) {        String tColor = "red";        String tBgcolor = "yellow";        if (color != null) tColor = color;        if (bgcolor != null) tBgcolor = bgcolor;        String spanStart = "<span style='color:" + tColor + ";background:" + tBgcolor +";'>";        String spanEnd = "</span>";        Pattern p = Pattern.compile(find, Pattern.CASE_INSENSITIVE);        Matcher m = p.matcher(source);        StringBuffer sb = new StringBuffer();        while (m.find()) {            m.appendReplacement(sb, spanStart + m.group() + spanEnd);        }        m.appendTail(sb);        return sb.toString();    }        // ------------------------------------------------------------substring        /**     * 截取字符串     * @param str 原始字符串     * @param size 截取的长度     * @return 新的字符串     */    public static String subString(String str, int start, int size) {        if (str.length() <= (start + size))             return str.substring(start);        return str.substring(start, start + size);    }        /**     * 清除Html标签     * @param str     * @return     */    public static String clearHtml(String str) {        int flag1 = str.indexOf("<");        int flag2 = str.indexOf(">");        String temp = null;        while (flag1 != -1 || flag2 != -1) {            str = clearHtmlPrivate(str);            flag1 = str.indexOf("<");            flag2 = str.indexOf(">");        }        return str;    }        private static String clearHtmlPrivate(String str) {        int start = str.indexOf("<");        int end = str.indexOf(">");        if (start != -1 || end != -1) {            str = str.substring(0, start) + str.substring(end + 1);        }        return str;    }        /**     * 根据给定的URL地址获取HtmlCode     * @param urlStr     * @return      */    public static String getHtmlCode(String url) throws MalformedURLException, IOException {        StringBuilder sb = new StringBuilder();        URL u = new URL(url);        InputStream in = u.openStream();        InputStreamReader isr = new InputStreamReader(in);        char[] buff = new char[2048];        int len;        while ((len = isr.read(buff, 0, buff.length))!= -1) {            sb.append(buff, 0, len);        }        isr.close();        return sb.toString();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -