java中如何在未知长度字符串中截取一段字符

C:\Users\Administrator\Desktop\my\企业宣传网站\web\WebRoot\images\botton-cz.jpg这段字符串中如何截取images\后的字符串,除去lastIndexOf("\")外的方法

java中截取未知长度字符串主要是使用String类,示例如下:

/**
 * @author cn
 * @param s è¦æˆªå–的字符串
 * @param length è¦æˆªå–字符串的长度->是字节一个汉字2个字节
 * return è¿”回length长度的字符串(含汉字)
*/
private static String getTitleToTen(String s, int length) throws Exception
    {

        byte[] bytes = s.getBytes("Unicode");
        int n = 0;
        int i = 2;
        for (; i < bytes.length && n < length; i++){
         if (i % 2 == 0){
                n++;
            }else{
                if (bytes[i] != 0){
                    n++;
                }
            }
        }
        /*if (i % 2 == 1){
            if (bytes[i - 1] == 0)
                i = i - 1;
            else
                i = i + 1;
        }*/
        //将截一半的汉字要保留
        if (i % 2 == 1){
         i = i + 1;
        }
        String eside = ".................................................................";
        byte[] byteEside = eside.getBytes("Unicode");
        String title = "";
        if (bytes[i-1] == 0){
         title = new String(bytes, 0, i, "Unicode")+new String(byteEside,0,40,"Unicode");
        }else{
         title = new String(bytes, 0, i, "Unicode")+new String(byteEside,0,38,"Unicode");
        }
        return title;
    }
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-08
String[] str=s.split("\");//s是上面字符串
String s1=str[str.length-1];//s1是得到的字符串
第2个回答  2015-09-17

不多说,上代码。用到截取子字符串函数substring(beginIndex, endIndex),用Math.random随机生成beginIndex, endIndex这两个参数。而这两个参数需要满足一定条件,开始索引要小于结束索引、两者不能相等、不能超出字符串长度、开始索引不能等于字符串长度、结束索引不能为0这些条件。

第3个回答  2013-08-08
String s = "C:\Users\Administrator\Desktop\my\企业宣传网站\web\WebRoot\images\botton-cz.jpg";

String img = s.substring(s.indexOf("images\\"));

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网