栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java在长字符串中查找短字符串的实现多种方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java在长字符串中查找短字符串的实现多种方法

方案一:

补充:在输入短字符串时,如果有空格,可以在比较前用 trim()方法截取前后空白

 
    System.out.println("请输入字符串:");
    Scanner str1=new Scanner(System.in);
    String s=str1.nextLine();
    System.out.println("请输入第二个字符串:");
    Scanner str2=new Scanner(System.in);
    String s2=str2.next();
    String[] i= s.split(" ");//对长字符串进行分割得到一个字符串数组
    int o=0;
    for (int j = 0; j 

方案二:

 //如果替换未造成字符串长度损失,该方法则不适用
    System.out.println("请输入一个长字符串:");
    Scanner str1 = new Scanner(System.in);
    String s = str1.nextLine();
    System.out.println("请输入短字符串:");
    String s1 = str1.nextLine();
    String s3 = s.replaceAll(s1, "0");//字符替换
    int b1 = s.length() - s3.length();//计算出s字符串损失的长度
    int b2 = b1 / (s1.length() - 1);//根据规律计算出s1字符串在s字符串中出现的次数
    System.out.println("次数为:"+b2);
方案三:
  //该方法适用于各种模式
    System.out.println("请输入一个长字符串:");
    Scanner str1 = new Scanner(System.in);
    String s = str1.nextLine();
    System.out.println("请输入短字符串:");
    String s1 = str1.nextLine();
    int c=0;
    for (int i = 0; i 

到此这篇关于Java在长字符串中查找短字符串的实现多种方法的文章就介绍到这了,更多相关Java 查找字符串内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/129752.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号