我想查找一個字符串是否包含空格,並且是否打印出錯誤。一旦找到空格,我必須使用substring()打印出一行之前的空格之前的所有內容,以及第二行之後的空格。這是我曾嘗試:查找一個字符串是否包含「」
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(字符串序列);'' – 3kings