Java 查找字符串是否包含&引用;
我试图找出字符串是否包含空格,以及它是否打印出错误。找到空格后,我必须使用substring()打印出一行空格前的所有内容,以及第二行空格后的所有内容。以下是我尝试过的:Java 查找字符串是否包含&引用;,java,string,java.util.scanner,Java,String,Java.util.scanner,我试图找出字符串是否包含空格,以及它是否打印出错误。找到空格后,我必须使用substring()打印出一行空格前的所有内容,以及第二行空格后的所有内容。以下是我尝试过的: while (type.hasNext()) { String word = type.nextLine(); // save the word that is tped into variable 'word' String nextLine = word; // save the 'w
while (type.hasNext())
{
String word = type.nextLine(); // save the word that is tped into variable 'word'
String nextLine = word; // save the 'word' as variable 'nextLine'
String check = " "; //save the white space that is typed as 'check'
int space = check.indexOf(' '); //check the index of space
if (word.contains(" "))
{
System.out.println(word);
String test = check.substring(0, space);
System.out.println(word);
String test2 = check.substring(space, word.length());
System.out.println(word);
}
else
System.out.println("ERROR: There is no space!"); }
感谢您的帮助就这么简单:
String.contains(" "); [where String is the name of your String variable].
就这么简单:
String.contains(" "); [where String is the name of your String variable].
就这么简单:
String.contains(" "); [where String is the name of your String variable].
就这么简单:
String.contains(" "); [where String is the name of your String variable].
这不是
indexOf()
的工作方式。是吗
返回:
此对象表示的字符序列中第一次出现的字符的索引,如果字符未出现,则为-1
if(检查索引(“”)<0)
{
System.out.println(“错误:没有空格!”);
}
这不是indexOf()
的工作方式。是吗
返回:
此对象表示的字符序列中第一次出现的字符的索引,如果字符未出现,则为-1
if(检查索引(“”)<0)
{
System.out.println(“错误:没有空格!”);
}
这不是indexOf()
的工作方式。是吗
返回:
此对象表示的字符序列中第一次出现的字符的索引,如果字符未出现,则为-1
if(检查索引(“”)<0)
{
System.out.println(“错误:没有空格!”);
}
这不是indexOf()
的工作方式。是吗
返回:
此对象表示的字符序列中第一次出现的字符的索引,如果字符未出现,则为-1
if(检查索引(“”)<0)
{
System.out.println(“错误:没有空格!”);
}
String.contains(字符串序列)代码>String.contains(字符串序列)代码>String.contains(字符串序列)代码>String.contains(字符串序列)代码>contains(“”)
是一个子字符串搜索,效率不如indexOf(“”)!=-1
,它同样易于编写和理解。@Andreas不,它不是。。。public boolean contains(CharSequence s){return indexOf(s.toString())>-1;}这是直接从string类的java源代码中获取的。您忽略了indexOf(“”)
和indexOf(“”
之间的区别。第一个是字符搜索,比第二个的子字符串搜索效率更高。contains(“”
是子字符串搜索,效率不如indexOf(“”)!=-1
,它同样易于编写和理解。@Andreas不,它不是。。。public boolean contains(CharSequence s){return indexOf(s.toString())>-1;}这是直接从string类的java源代码中获取的。您忽略了indexOf(“”)
和indexOf(“”
之间的区别。第一个是字符搜索,比第二个的子字符串搜索效率更高。contains(“”
是子字符串搜索,效率不如indexOf(“”)!=-1
,它同样易于编写和理解。@Andreas不,它不是。。。public boolean contains(CharSequence s){return indexOf(s.toString())>-1;}这是直接从string类的java源代码中获取的。您忽略了indexOf(“”)
和indexOf(“”
之间的区别。第一个是字符搜索,比第二个的子字符串搜索效率更高。contains(“”
是子字符串搜索,效率不如indexOf(“”)!=-1
,它同样易于编写和理解。@Andreas不,它不是。。。public boolean contains(CharSequence s){return indexOf(s.toString())>-1;}这是直接从string类的java源代码中获取的。您忽略了indexOf(“”)
和indexOf(“”
之间的区别。第一个是字符搜索,它比第二个的子字符串搜索更有效。