/*Match pattern ab in the pattern ababbaba and print the number of times ab is in the pattern , and also print the index value of a in ab*/
package stringmatch;
public class StringMatch {
public static void main(String[] args) {
String name="ababbaba";
int len,index=0,count=0,temp=0,in,i=index+1;
String m;
char ch,c;
// String match="ab";
len=name.length();
for(index=0;index<=len-1;index++)
{
ch=name.charAt(index);
if(name.charAt(index)=='a'&&name.charAt(i+1)=='b')
{
count++;
temp=index;
System.out.println("ab is "+count+" "+"index is "+temp);
//i++;
}
}
} //System.out.println(count+" ");
}
答案 0 :(得分:1)
您可以按以下方式重写代码,
String name="ababbaba";
int len, count=0;
// String match="ab";
len=name.length();
for(int index=0; index<=len-2;index++)
{
if(name.charAt(index)=='a' && name.charAt(index+1)=='b')
{
count++;
System.out.println("ab is "+count+" "+"index is "+ index);
}
}