public static void main(String[] args) {
String str = "asfasfnabaasdfnbassdnbasnbasdnbadfasdfnba";
String key = "nba";
int startindex = 0;
int count = 0;
while ((startindex = str.indexOf(key, startindex)) = 0) {
count++;
System.out.println("位置:" + startindex);//可以提前声明一个数组用来存放
startindex += key.length();
}
System.out.println("总数" + count);
可以用正则表达式:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class DemoAsm{
public static void main(String[] args){
String str="abcdef";//输入字符串
String reg="cde";//需要查找的字符串
Pattern pattern =Pattern.compile(reg);
Matcher mather=pattern.matcher(str);
while(mather.find()){
System.out.println(mather.group());//如果有,则输出
public?class?Strings{
public?static?void?main(String[]?args){
String?str?=?"字符串";
if(str.indexOf("串")?!=?-1){
System.out.println("包含串子");
}else{
System.out.println("不包含串子");
System.out.println("删除后为:"?+?str.replace("串",""));
System.out.println("替换后为:"?+?str.replace("串","!"));
String s = new Scanner(System.in).nextLine();
System.out.println("输入查找的字符:");
String s1 = new Scanner(System.in).nextLine();
这里s,s1是局部变量啊
改成
s = new Scanner(System.in).nextLine();
s1 = new Scanner(System.in).nextLine();
以上就是土嘎嘎小编为大家整理的找出子串位置java代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!