不耻下问:Java语言怎么实现类似C++的strstr函数的功能,有没有对应的功能可以调用呢
2条回答 默认 最新
- 这不是鸭头 2020-10-12 10:57关注
import java.util.Scanner; /** * 实现strStr()函数,给定haystack字符串和一个needle字符串, * 在haystack字符串中找出needle字符串出现的第一个位置(从0开始)。如果不存在,则返回-1。 */ public class Realize_strStr { public static void main(String[] args) { System.out.println("请输入haystack字符串:"); String haystack = new Scanner(System.in).nextLine(); System.out.println("请输入needle字符串"); String needle = new Scanner(System.in).nextLine(); System.out.println("结果为:"+Solution(haystack,needle)); } public static int Solution(String haystack, String neddle){ for (int i = 0; i < haystack.length()-neddle.length() + 1; i++) { //从0开始,到haystack剩余长度小于neddle为止 if (haystack.substring(i,i+neddle.length()).equals(neddle)) return i;//判断到此为止haystack内是否存在neddle } return -1; } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 3无用
悬赏问题
- ¥15 程序不包含适用于入口点的静态Main方法
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记