急!50分求一个截取字符串问题!

AI老叫瘦 2006-05-10 02:55:03
从指定字符串开始截取到指定字符串,应该怎么写?有明白的快回啊~~~~
----------------------------
...全文
569 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
overgame 2006-05-11
  • 打赏
  • 举报
回复
to icefire1512(花花)
大哥,我是在判断是不是以“c.”开头,不是在判断是否存在“c”这个字符:)
------------------------------

如果出现cxsads.dd= 'xxx'的话,你怎么办。也算?
这种以字符判断的方法本身就是不合理的,麻烦严格一点对待开发好不好
lendoxp 2006-05-11
  • 打赏
  • 举报
回复
reg.compile("c\\.+and");
chaosking 2006-05-10
  • 打赏
  • 举报
回复
最简单的方法:
String s = "g.dah='1234567899' and c.lx='西医' ";
String s1 = s.substring(s.indexOf("c."));
String target = null;
if(s1.indexOf("' ") == -1){
target = s1;
}else {
target = s1.substring(0,s1.indexOf("' ")+1);
}
应该可以了,没时间调了,你自己测一下。
AI老叫瘦 2006-05-10
  • 打赏
  • 举报
回复
正则表达式我不太会用~有没有比较好的例子呢?:)
icefire1512(花花) 的方法我觉得比较符合我的想法~~~~
qinqinxiatiao 2006-05-10
  • 打赏
  • 举报
回复
对字符串的操作还是正则表达式更合适一些 ^_^
AI老叫瘦 2006-05-10
  • 打赏
  • 举报
回复
这个方法比较不错!!!
icefire1512 2006-05-10
  • 打赏
  • 举报
回复
可以把 if(strArray[i].indexOf("c")==0)改成 if(strArray[i].indexOf("c.")==0)
这样更严谨一些
icefire1512 2006-05-10
  • 打赏
  • 举报
回复
大哥,我是在判断是不是以“c.”开头,不是在判断是否存在“c”这个字符:)
overgame 2006-05-10
  • 打赏
  • 举报
回复
String str = "g.dah='1234567899' and c.lx='西医' and c.a='a' and c.b='b'";
String[] strArray=str.split(" and ");
Vector wannaStrings=new Vector();
for(int i=0;i<strArray.length;i++){
if(strArray[i].indexOf("c")==0){
wannaStrings.add(strArray[i]);
}
}
在wannaStrings这个Vector中就是你想要的以“c.”开头的东西,如果需要你再自己把组字符串吧
----------------------

这个方法设计本身就有问题,如果我的
String str = "g.dah='1234567899' and c.lx='西医' and c.a='a' and c.b='b'";
改成
String str = "g.dch='1234567899' and c.lx='西医' and c.a='a' and c.b='b' and bb.ca='sss'";怎么办?
你按and分割,然后只判断是否存在c就放进去了,那样很容易出错.


我试了下正则,因为条件太过于模糊,没有办法确认到很精确的数据,建议楼主把需求提出来,建议采用变通的实现方式
AI老叫瘦 2006-05-10
  • 打赏
  • 举报
回复
此方法我考虑一下!~~
icefire1512 2006-05-10
  • 打赏
  • 举报
回复
String str = "g.dah='1234567899' and c.lx='西医' and c.a='a' and c.b='b'";
String[] strArray=str.split(" and ");
Vector wannaStrings=new Vector();
for(int i=0;i<strArray.length;i++){
if(strArray[i].indexOf("c")==0){
wannaStrings.add(strArray[i]);
}
}
在wannaStrings这个Vector中就是你想要的以“c.”开头的东西,如果需要你再自己把组字符串吧
liujiwe79 2006-05-10
  • 打赏
  • 举报
回复
String str = "g.dah='1234567899' and c.lx='西医'";

str.substring( str.lastIndexOf("and"),str.lastIndexOf("c"))
AI老叫瘦 2006-05-10
  • 打赏
  • 举报
回复
还有没有可以截取多个‘c’到‘and’的之间字符的方法啊!!!!!!!!!!!!!!!!!!!!!!!!!
AI老叫瘦 2006-05-10
  • 打赏
  • 举报
回复
如果有多个‘c’到‘and’可以都截取出来么?
AI老叫瘦 2006-05-10
  • 打赏
  • 举报
回复
这样写会提示:java.lang.StringIndexOutOfBoundsException:
zuguanqun 2006-05-10
  • 打赏
  • 举报
回复
String str = "g.dah='1234567899' and c.lx='西医'";

str.substring( str.lastIndexOf("c"), str.lastIndexOf("and"))
AI老叫瘦 2006-05-10
  • 打赏
  • 举报
回复
因为比较急 最好有差不多的方法
AI老叫瘦 2006-05-10
  • 打赏
  • 举报
回复
正则表达式应该怎么用?可以给点提示么?
yeahspyme 2006-05-10
  • 打赏
  • 举报
回复
可以用正则表达式匹配
参考java.util.regex
AI老叫瘦 2006-05-10
  • 打赏
  • 举报
回复
现在有一段字符串~数据库条件~例如g.dah='1234567899' and c.lx='西医'。我想截取‘c.lx='西医'’它,在这个字符串条件中所有以c.开头的到下一个and结束的字符
加载更多回复(3)

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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