有字符串abcd efg abc,我要查找abc出现的位置,要是匹配到完整一个abc单词才返回``如果匹配到一个串的一部分则不返回``完整的一个abc是根据两边的空格而定`是匹配一个指定的单词才返回``请问怎样写?我再说明白点`单词的长度不定,有可能不是3个`
解决方案 »
- java类的最后要加分号吗
- swing 程序如何访问URL地址
- 如何实现编辑器的查询和替换功能?我的保存为什么不起作用?该如何做?
- 逻辑算法,在线等!!!!!!!!!!!急啊!!!!!!!!!!!!!
- SOCKET连接和收发数据的问题
- 用jxl将多个单sheet的Excel,合并成含多个sheet的一个Excel中的问题
- 求助这段AWT程序,运行时除生成有菜单的主窗口外,还生成了一个多余的空白窗口
- 为什么在编译器找不到同一个目录下定义的类??
- 谁能回答我关于 Junit 的问题
- 我想要学习Java,可是不知如何下手,还请各位指点!
- swing的JFileChooser组件可否控制字体?
- 如何将鼠标限制在某个组件的区域内?
public static void main(String args[]){
String src = "abcd efg abc";
String match = "abc";
int off;
if((off = src.indexOf(" "+match+" ")) !=-1){
System.out.println(off);
}
else if((off = src.indexOf(" "+match)) !=-1){
System.out.println(off);
}
else{
System.out.println("no matcher!");
}
}
}
public class MatchStr {
public static void main(String args[]){
String src = "abc dlaj abc abcd abc abc";
String match = "abc";
StringTokenizer s = new StringTokenizer(src);
String word;
boolean isMatch = false;
int off = 0;
while(s.hasMoreTokens()){
word = s.nextToken();
if(word.equals(match)) {
System.out.println(off);
isMatch = true;
}
off += word.length()+1;
}
if(isMatch == false) {
System.out.println("no match!");
}
}
}
public class MatchStr {
public static void main(String args[]){
String src = "abc dlaj abc abcd abc abc";
String match = "abc";
int i = 0;
boolean isMatch = false;
if(src.startsWith(match+" ")==true){
System.out.println(i);
}
while(i<src.length()){
if(src.startsWith(" "+match+" ",i)==true){
System.out.println(i+1);
i = i+ 4;
isMatch = true;
continue;
}
else if(src.startsWith(" "+match,i)==true&&i == (src.length()-4)){
System.out.println(i+1);
isMatch = true;
break;
}
i++;
}
if(isMatch== false){
System.out.println("no matcher!");
}
}
}