以下代码将打印2
String word = "bannanas";
String guess = "n";
int index;
System.out.println(
index = word.indexOf(guess)
);
Run Code Online (Sandbox Code Playgroud)
我想知道如何在字符串"bannanas"中获取"n"("guess")的所有索引
预期结果将是: [2,3,5]
在Go中,我通常将我的JSON解组为结构并从结构中读取值...它非常有效.
这次我只关心JSON对象的某个元素,因为整个JSON对象非常大,我不想创建一个结构.
Go中是否有一种方法可以使用键或迭代数组按常规查找JSON对象中的值.
考虑到以下JSON,我怎么才能拔出该title字段.
{
"title": "Found a bug",
"body": "I'm having a problem with this.",
"assignee": "octocat",
"milestone": 1,
"labels": [
"bug"
]
}
Run Code Online (Sandbox Code Playgroud) 我需要在字符串中获取第一个字母的位置.
按照一个基本的例子.第一个字母是T,所以我需要知道T我可以使用的位置indexOf("T").
我想通过正则表达式获得第一个字母,而不是添加硬编码的字母(T).
提前致谢.
public class RegexMatches {
public static void main(String args[]) {
String line = "000 1 This is my message";
int first = line.indexOf("T");
line = line.substring(first, line.length());
System.out.println(line);
}
}
Run Code Online (Sandbox Code Playgroud) 我必须在字符串中找到*的出现,并且基于字符串中*的位置,必须执行某些操作.
if(* found in the beginning of the String) {
do this
}
if(* found in the middle of the String) {
do this
}
if(* found at the end of the String) {
do this
}
Run Code Online (Sandbox Code Playgroud)
我使用了matcher.find()选项,但它没有给出所需的结果.
在这里我不明白"al"的索引如何获得4的输出,因为a和l不在一起,对于所有其他组合,它给出-1.
package javaBasics1;
public class MainMethod {
public static void main(String name[]){
int x = 1,y = 5;
String a = "animals";
System.out.println(a.indexOf("al"));
}
}
Run Code Online (Sandbox Code Playgroud)