输出给定子串在字符串中出现的所有位置,如无此串,显示“no this string!” Input 输入两个字符串str1,str2。第一个字符串str1为源字符串,str2为待查找子串 。 Output 输出所有子串在源字符串中出现的位置,如果有多个,中间用分号隔开。如果不存在则输出“no this string!”。
时间: 2023-11-27 15:55:42 浏览: 38
以下是 Python 代码实现:
```python
str1 = input("请输入源字符串:")
str2 = input("请输入待查找子串:")
if str2 not in str1:
print("no this string!")
else:
positions = []
start = 0
while True:
position = str1.find(str2, start)
if position == -1:
break
positions.append(str(position))
start = position + 1
print(";".join(positions))
```
代码思路:
首先输入源字符串和待查找子串,如果待查找子串不在源字符串中,则输出“no this string!”;否则,我们使用 `find()` 方法查找待查找子串在源字符串中的位置,如果找到了,则将该位置添加到位置列表中,并将查找的起始位置移动到该位置之后,继续进行查找,直到无法找到为止。最后,我们将列表中的位置用分号拼接起来输出。
相关问题
java输出给定子串在字符串中出现的所有位置,如无此串,显示“no this string!”
可以使用Java中的String类提供的indexOf()方法来查找子串在字符串中出现的位置。具体实现如下:
```java
public static void findSubstring(String str, String subStr) {
int index = str.indexOf(subStr);
if (index == -1) {
System.out.println("no this string!");
} else {
while (index != -1) {
System.out.println(index);
index = str.indexOf(subStr, index + 1);
}
}
}
```
其中,indexOf()方法用于查找子串在字符串中第一次出现的位置,如果找不到,则返回-1。如果找到了,就将该位置输出,并继续查找下一个位置,直到找不到为止。如果一开始就找不到子串,就直接输出“no this string!”。
java 写一个方法在一个字符串的左边第一个给定子串后面加入一个子串
可以使用 Java 的字符串方法来实现在一个字符串的左边第一个给定子串后面加入一个子串。以下是一个示例代码:
```java
public class StringUtils {
public static String insertSubstring(String mainString, String targetSubstring, String insertSubstring) {
int index = mainString.indexOf(targetSubstring);
if (index == -1) {
return mainString;
}
StringBuilder sb = new StringBuilder();
sb.append(mainString.substring(0, index + targetSubstring.length()));
sb.append(insertSubstring);
sb.append(mainString.substring(index + targetSubstring.length()));
return sb.toString();
}
public static void main(String[] args) {
String mainString = "Hello World";
String targetSubstring = "World";
String insertSubstring = "CSDN";
String result = insertSubstring(mainString, targetSubstring, insertSubstring);
System.out.println(result);
}
}
```
上述代码中,`insertSubstring` 方法接收三个参数:`mainString` 是原始字符串,`targetSubstring` 是要查找的子串,`insertSubstring` 是要插入的子串。该方法首先使用 `indexOf` 方法找到第一个给定子串的位置,然后使用 `substring` 方法将原始字符串分成两部分,并在指定位置处插入子串。最后,使用 `toString` 方法将 `StringBuilder` 对象转换为字符串,并返回插入了子串的结果。
在示例代码中,我们将字符串 "Hello World" 的子串 "World" 后面插入了 "CSDN",输出结果为 "Hello WorldCSDN"。你可以根据自己的需求修改参数,并测试该方法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)