当前位置:网站首页>Enter two strings STR1 and STR2, and count the number of times that the string STR2 appears in STR1.
Enter two strings STR1 and STR2, and count the number of times that the string STR2 appears in STR1.
2022-07-23 06:03:00 【Seven percent】
Enter two strings str1、str2, Statistics string str2 Appear in the str1 Number of times in .
Such as :str1=”aaas lkaaas” ,str2=” as” , Then output 2.
package labreport7;
import java.util.Scanner;
// Enter two strings str1、str2, Statistics string str2 Appear in the str1 Number of times in .
public class test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Define the object of scanning keyboard input
String s1 = sc.nextLine(); // Read a line of text from the keyboard
String s2=sc.nextLine();
int i=0;// Index subscript
int sum=0;// Number of Statistics
while(s1.indexOf(s2,i)!=-1) {// from i Start lookup at index , Returns the index position of the first occurrence of a specified string value in the string . If not, return -1, If yes, the index value is returned
i=s1.indexOf(s2,i)+1;// Index value plus one , Then look back
sum++;
}
System.out.println(sum);
sc.close();
}
}

public int indexOf(String str,from Index): Find the index of the first occurrence of the returned character in the string from the specified position , such as “woaizhongguo”indexOf('o',2) The return value is 6 instead of 1, Neither 11; If not, return -1.
public int lastIndexOf(String str): Returns the index of the last occurrence of the specified character in this string , If there is no such character in this string , Then return to -1.
边栏推荐
猜你喜欢

基于知识图谱的职位推荐系统的设计与实现

最大公约数和最小公倍数

UNIX编程—网络socket

Unix programming project - the client based on raspberry pie regularly obtains the temperature and reports it to the server

mysql数据库基本知识

Shell basic commands

PXE高效批量网络装机

两个及其简单的TCPUDP程序,树莓派与pc间的通信

第四次作业:关于cat,grep,cut,sort,uniq,vim,tr等命令的用法

嵌入式系统移植【2】——交叉开发环境的搭建
随机推荐
浅谈LoRa,LoRaWAN,NB-IoT三类物联网技术
stored procedure
Fiddler Script个性化配置显示
NFS共享服务
Day 3 summary & homework
移动应用分类
IA笔记2
指针学习日记(一)
Common problems of multiple processes - how to lock the same parent thread variable (critical resource) when creating multiple threads so that the shared parent thread variable is not repeatedly modif
Jmeter正则表达式提取器
get请求和post请求区别
UNIX实现IO多路复用之使用epoll函数实现网络socket服务端
On Lora, lorawan and Nb IOT Internet of things technologies
A little life
Asset mapping process
Regular expression I
LIinux下的基本C编程的三类高频函数操作详解第一类——文件操作函数(f)
Introduction to programming 2 - find the minimum value
详解虚拟机下三种联网模式
嵌入式系统移植【1】————导学