Java实现在字符串中查找字符串
String A = "... ,...." 问题是,我想找出在所有的hello-x中,hello-a是第几个?(也就是应该是3)。String A是一很长很长的字符串,hello-x的前后两部分都还有很多字符的。。。
使用index函数,连续查找hello-字符串,然后判断下一个字符是不是c如果是就停止,这就是个数了 下面是例子: String B = "hello-"; int index = A。indexOf(B, 0); int cnt = 0; while (index >=0){ if(A。
charAt(index+B。length()) != 'c'){ cnt ++; }else{ System。out。println("CNT="+(cnt+1)); break; } index = A。
indexOf(B,index+B。length()); } 。
就是查找子字符串麻 关键是hello-是一定的所以只要知道截取hello-c之前截取了几次hello-就可以了 具体程序就是查找子字符串外加个for就不在这里?铝?
答:可以用这方法解决: 原型:public boolean regionMatches(int toffset,String other,int ooffset,i...详情>>
答:google "java 缓冲池"详情>>
答:百度里搜张孝祥 非常好的视频教程详情>>