java截取字符串问题

无锡阔微信息科技 2012-11-21 10:41:06
字符串:行政类@行政1#行政2/人事类@人事1#人事2/财务类@财务1#财务2/采购类/生产类/技术类/市场类/售后类/物流类/其他

现在想要的结果是:
以@开头的得到:行政1和人事1
以#开头的得到:行政2和人事2
已/开头的得到:行政类,人事类,财务类,采购类,生产类,技术类,市场类,售后类,物流类,其他

麻烦各位牛人了


...全文
195 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dracularking 2012-11-21
  • 打赏
  • 举报
回复
>以@开头的得到:行政1和人事1 >以#开头的得到:行政2和人事2 >已/开头的得到:行政类,人事类,财务类,采购类,生产类,技术类,市场类,售后类,物流类,其他 因为用@,#开头的你只要前两个,直接用String的indexOf定位可以 后面的以/开头的是要所有,可以先将所有非/开头的都替换掉,只剩/开头的,再分一下就ok了
zxhcloth 2012-11-21
  • 打赏
  • 举报
回复
如果你是想要把"\","#","@"符号去掉的话(也就是结果不包含这些符号),用以下代码:

String s = "/行政类@行政1#行政2/人事类@人事1#人事2/财务类@财务1#财务2/采购类/生产类/技术类/市场类/售后类/物流类/其他";
        Pattern p = Pattern.compile("/([\\w\\u4e00-\\u9fa5]+)");
        Matcher m = p.matcher(s);
        while(m.find()) {
            System.out.println(m.group(1));
        }
        
        Pattern p1 = Pattern.compile("@([\\w\\u4e00-\\u9fa5]+)");
        Matcher m1 = p1.matcher(s);
        while(m1.find()) {
            System.out.println(m1.group(1));
        }
        
        Pattern p2 = Pattern.compile("#([\\w\\u4e00-\\u9fa5]+)");
        Matcher m2 = p2.matcher(s);
        while(m2.find()) {
            System.out.println(m2.group(1));
        }
zxhcloth 2012-11-21
  • 打赏
  • 举报
回复
专门给你写了这个正则:

String s = "/行政类@行政1#行政2/人事类@人事1#人事2/财务类@财务1#财务2/采购类/生产类/技术类/市场类/售后类/物流类/其他";
        Pattern p = Pattern.compile("/[\\w\\u4e00-\\u9fa5]+");
        Matcher m = p.matcher(s);
        while(m.find()) {
            System.out.println(m.group(0));
        }
        
        Pattern p1 = Pattern.compile("@[\\w\\u4e00-\\u9fa5]+");
        Matcher m1 = p1.matcher(s);
        while(m1.find()) {
            System.out.println(m1.group(0));
        }
        
        Pattern p2 = Pattern.compile("#[\\w\\u4e00-\\u9fa5]+");
        Matcher m2 = p2.matcher(s);
        while(m2.find()) {
            System.out.println(m2.group(0));
        }
zoeg 2012-11-21
  • 打赏
  • 举报
回复
串行二维表,你重新解析成二维表以后,想怎么取数据就怎么取数据!
有一只柴犬 2012-11-21
  • 打赏
  • 举报
回复
String str = "行政类@行政1#行政2/人事类@人事1#人事2/财务类@财务1#财务2/采购类/生产类/技术类/市场类/售后类/物流类/其他"; 以@开头的得到:String s = ""; String[] str1 = str.split("@"); for(int i=0;i<str1.length;i++){ s += str1[i].split("#")[0].split("/")[0]+","; } 其他的差不多也是这样,你试下看看
悲催的程序猿 2012-11-21
  • 打赏
  • 举报
回复
如果没有要求的话这题目就简单,实在不会你数下标也行。

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧