java中,编写一个程序让用户分别输入字符串和子字符串,输出子字符串出现的次数。编写一个程序让用户分别输入字符串和子字符串,输出子字符串出现的次数。
时间: 2024-04-30 09:23:28 浏览: 20
下面是Java代码实现:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入字符串:");
String str = scanner.nextLine();
System.out.print("请输入子字符串:");
String subStr = scanner.nextLine();
int count = 0;
int index = str.indexOf(subStr);
while (index != -1) {
count++;
index = str.indexOf(subStr, index + 1);
}
System.out.println("子字符串出现的次数为:" + count);
}
}
```
运行程序,依次输入字符串和子字符串,即可得到子字符串出现的次数。例如,输入字符串为"hello world, hello java",子字符串为"hello",则输出结果为"子字符串出现的次数为:2"。
相关问题
java中,计算字符串中子字符串出现的次数,编写一个程序让用户分别输入字符串和子字符串,输出子字符串出现的次数,使用 Scanner 对象进行输入字符串时请使用 next() 方法,而非 nextLine() 方法。
可以使用 String 类的 indexOf() 方法来查找子字符串在字符串中的位置,然后不断循环查找并计数即可。
以下是一个示例代码:
```java
import java.util.Scanner;
public class SubstringCount {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入字符串:");
String str = scanner.next();
System.out.print("请输入子字符串:");
String sub = scanner.next();
int count = 0;
int index = str.indexOf(sub);
while (index >= 0) {
count++;
index = str.indexOf(sub, index + 1);
}
System.out.println("子字符串出现的次数为:" + count);
}
}
```
这里使用了一个 while 循环来查找子字符串的位置,如果找到了则计数器加一,然后将查找的起始位置更新为当前位置加一。当找不到子字符串时,indexOf() 方法会返回负数,循环终止。
注意在输入字符串和子字符串时使用了 next() 方法,而不是 nextLine() 方法。这是因为 nextLine() 方法会读取输入中的一整行,而我们只需要读取一个字符串,因此使用 next() 方法更合适。
编写一个Java应用程序,程序输出字符串中与指定模式匹配的子字符串
可以使用Java中的正则表达式来实现字符串的匹配。以下是一个示例程序,演示了如何输出字符串中与指定模式匹配的子字符串:
```java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringMatching {
public static void main(String[] args) {
String inputString = "This is a sample string with some patterns to match.";
String pattern = "pattern"; // 指定的模式
// 使用正则表达式进行匹配
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(inputString);
// 输出匹配到的子字符串
while (m.find()) {
System.out.println("Match found at index " + m.start() + " to " + (m.end()-1));
System.out.println(m.group());
}
}
}
```
在上面的示例程序中,我们定义了一个字符串 `inputString` 和一个指定的模式 `pattern`。然后使用 `Pattern` 类的 `compile()` 方法将模式编译成一个正则表达式对象,并使用 `Matcher` 类的 `matcher()` 方法来匹配字符串。使用 `find()` 方法来查找字符串中是否存在与模式匹配的子字符串,如果找到了,则使用 `start()` 和 `end()` 方法来获取匹配的子字符串在原始字符串中的起始位置和结束位置,并使用 `group()` 方法来获取匹配的子字符串本身。最后,我们可以将匹配到的子字符串输出到控制台。
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)