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

📄 stringutil.java

📁 java 对string的各种函数
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        {
            return Long.parseLong(strName);
        }
        catch(Exception e)
        {
            return defaultvalue;
        }
    }

    public static final String getString(String s, int length)
    {
        if(s == null)
            return null;
        if(getNumofByte(s) <= length)
            return s;
        byte bb[] = s.getBytes();
        byte temp[] = new byte[length];
        for(int i = 0; i < length; i++)
            temp[i] = bb[i];

        return getString(temp);
    }

    private static int getNumofByte(String s)
    {
        if(s == null)
            return 0;
        else
            return s.getBytes().length;
    }

    private static final String getString(byte bb[])
    {
        String s = new String(bb);
        if(s.length() == 0)
        {
            int length = bb.length;
            if(length > 1)
            {
                byte temp[] = new byte[length - 1];
                for(int i = 0; i < length - 1; i++)
                    temp[i] = bb[i];

                return getString(temp);
            } else
            {
                return "";
            }
        } else
        {
            return s;
        }
    }

    public static final String getMD5(String s)
    {
        if(s == null)
            return null;
        if(s.trim().length() == 0)
        {
            return s;
        } else
        {
            Md5 md5 = new Md5();
            return md5.getMD5ofStr(s);
        }
    }

    public static final int getInt(HttpServletRequest request, String ParamName)
    {
        return getInt(request, ParamName, 0);
    }

    public static final int getInt(HttpServletRequest request, String ParamName, int defV)
    {
        if(request.getParameter(ParamName) != null)
            try
            {
                int t = Integer.parseInt(request.getParameter(ParamName));
                return t;
            }
            catch(NumberFormatException e)
            {
                return 0;
            }
        else
            return defV;
    }

    public static final long getLong(HttpServletRequest request, String ParamName)
    {
        if(request.getParameter(ParamName) != null)
            try
            {
                long t = Long.parseLong(request.getParameter(ParamName).trim());
                return t;
            }
            catch(NumberFormatException e)
            {
                return 0L;
            }
        else
            return 0L;
    }

    public static final long getLong(HttpServletRequest request, String ParamName, long def)
    {
        if(request.getParameter(ParamName) != null)
            try
            {
                long t = Long.parseLong(request.getParameter(ParamName));
                return t;
            }
            catch(NumberFormatException e)
            {
                return 0L;
            }
        else
            return def;
    }

    public static final String getString(HttpServletRequest request, String paramName)
    {
        if(paramName == null)
            return null;
        String t = request.getParameter(paramName);
        if(t != null && !t.equals(""))
            return t.trim();
        else
            return "";
    }

    public static final String getString(HttpServletRequest request, String paramName, String defaultVal)
    {
        String t = request.getParameter(paramName);
        if(t != null && !t.equals(""))
            return t.trim();
        else
            return defaultVal;
    }

    public static final HttpSession getSession(HttpServletRequest request)
    {
        return request.getSession(true);
    }

    public static final String cutString(String str, int len, String tail)
    {
        int slen = str.length();
        if(slen > len)
            str = str.substring(0, len) + tail;
        return str;
    }

    public static String formatNumber(String formatstr, double number)
    {
        DecimalFormat df = new DecimalFormat(formatstr);
        return df.format(number);
    }

    public static String formatNumber(String formatstr, long number)
    {
        DecimalFormat df = new DecimalFormat(formatstr);
        return df.format(number);
    }

    public static final String getBackURL(HttpServletRequest request)
    {
        String backurl = getString(request, "backurl");
        backurl = replaceString(backurl, "@", "");
        if(backurl.equals(""))
        {
            String ref = request.getHeader("referer");
            if(ref == null)
                ref = "";
            return ref;
        } else
        {
            return backurl;
        }
    }

    public static String getCurrentURL(HttpServletRequest request)
    {
        String paras = request.getQueryString();
        if(paras == null)
            return request.getRequestURI();
        else
            return request.getRequestURI() + "?" + paras;
    }

    public static String enCodeBackURL(String backurl)
    {
        return backurl.replace('&', '!');
    }

    public static String deCodeBackURL(String backurl)
    {
        return backurl.replace('!', '&');
    }

    public static long bin2Dec(String bin)
    {
        int binLen = bin.length();
        long sum = 0L;
        for(int i = 0; i < binLen; i++)
        {
            int bit = getInt(bin.substring(binLen - i - 1, binLen - i));
            sum = (long)((double)sum + (double)bit * Math.pow(2D, i));
        }

        return sum;
    }

    public static String getStatusForCheckBoxAndRadio(int i)
    {
        if(i == 0)
            return "";
        else
            return "checked";
    }

    public static void setCookie(HttpServletResponse response, String CookieName, String CookieVal, int CookieAge)
    {
        Cookie cookie = new Cookie(CookieName, URLEncoder.encode(CookieVal));
        cookie.setMaxAge(CookieAge);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    public static String getCookie(HttpServletRequest request, String CookieName)
    {
        Cookie cookies[] = request.getCookies();
        if(cookies == null)
            return null;
        for(int i = 0; i < cookies.length; i++)
            if(cookies[i].getName().equals(CookieName))
                return URLDecoder.decode(cookies[i].getValue());

        return null;
    }

    public static String[] getCookie(HttpServletRequest request)
    {
        Cookie cookies[] = request.getCookies();
        ArrayList al = new ArrayList();
        if(cookies == null)
            return null;
        for(int i = 0; i < cookies.length; i++)
            al.add(cookies[i].getName() + " = " + URLDecoder.decode(cookies[i].getValue()));

        return (String[])al.toArray(new String[0]);
    }

    public static String removeHH(String str)
    {
        return replaceString(replaceString(str, "\n", ""), "\r", "");
    }

    public static String readURL(String Inputurl)
        throws Exception
    {
        String sCurrentLine = "";
        String sTotalString = "";
        URL url = new URL(Inputurl);
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
        urlConnection.connect();
        java.io.InputStream urlInputStream = urlConnection.getInputStream();
        BufferedReader BufferReader = new BufferedReader(new InputStreamReader(urlInputStream));
        while((sCurrentLine = BufferReader.readLine()) != null) 
            sTotalString = sTotalString + sCurrentLine + "\n";
        BufferReader.close();
        return sTotalString;
    }

    public static String withColor(String osStr, String color, boolean flag)
    {
        if(flag)
            return "<font color='".concat(color).concat("'>").concat(osStr).concat("</font>");
        else
            return osStr;
    }

    public static String getStringFromSession(HttpSession ses, String name)
    {
        if(ses.getAttribute(name) != null)
            return ses.getAttribute(name).toString();
        else
            return null;
    }

    public static int getIntFromSession(HttpSession ses, String name)
    {
        if(getStringFromSession(ses, name) != null)
            return getInt(getStringFromSession(ses, name));
        else
            return 0;
    }

    public static float getFloatFromSession(HttpSession ses, String name)
    {
        if(getStringFromSession(ses, name) != null)
            try
            {
                return getFloat(getStringFromSession(ses, name));
            }
            catch(NumberFormatException e)
            {
                return 0.0F;
            }
            catch(Exception e)
            {
                return 0.0F;
            }
        else
            return 0.0F;
    }

    public static String getAllParameters(HttpServletRequest request)
    {
        Enumeration en = request.getParameterNames();
        StringBuffer backSb = new StringBuffer("");
        for(; en.hasMoreElements(); backSb.append("&"))
        {
            String paramName = (String)en.nextElement();
            String paramValue = request.getParameter(paramName);
            backSb.append(paramName);
            backSb.append("=");
            backSb.append(paramValue);
        }

        return backSb.toString();
    }

    public static void main(String args[])
        throws Exception
    {
        String t = readURL("http://leobasic:8082/en/appController/cgVenderLogin");
        System.out.println(t);
    }
}

⌨️ 快捷键说明

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