已知字符串,求字符串中前两个i的位置,以及最后一个a的位置java is a very interest ing programming language
时间: 2023-07-15 09:15:52 浏览: 47
代码如下:
```
public class Test {
public static void main(String[] args) {
String str = "java is a very interest ing programming language";
int firstI = str.indexOf("i");
int secondI = str.indexOf("i", firstI + 1);
int lastA = str.lastIndexOf("a");
System.out.println("第一个i的位置:" + firstI);
System.out.println("第二个i的位置:" + secondI);
System.out.println("最后一个a的位置:" + lastA);
}
}
```
输出结果为:
```
第一个i的位置:1
第二个i的位置:5
最后一个a的位置:46
```
相关问题
java中如何把一个已知的字符串数组转为字符串
在 Java 中,可以使用 `String.join()` 方法将一个已知的字符串数组转换为字符串。
例如:
```
String[] arr = {"apple", "banana", "cherry"};
String str = String.join(", ", arr);
```
此代码将创建一个字符串:`"apple, banana, cherry"`。
java取字符串中第一个出现已知字符的后一段字符串,请给出示例代码
以下是示例代码:
```java
public class Main {
public static void main(String[] args) {
String str = "Hello, world!";
char ch = ',';
int index = str.indexOf(ch);
if (index != -1) {
String result = str.substring(index + 1);
System.out.println(result); // 输出 " world!"
}
}
}
```
在这个示例中,我们首先定义了一个字符串 `str` 和一个字符 `ch`,然后使用 `indexOf()` 方法查找字符串 `str` 中第一次出现字符 `ch` 的位置。如果找到了,我们就使用 `substring()` 方法从该位置的下一个字符开始提取子字符串,最后将结果输出到控制台。注意,`substring()` 方法的参数是子字符串的起始位置,所以我们需要将 `index` 加 1。