如果 String .contains 方法不适合,如何 return null?
How to return null if String .contains method doesn't fit?
我有此代码,如果没有标题包含 wordInTitle 的书,或者如果 wordInTitle 为 null 或“”,我想 return null。我可以设法为 null 和 "" 做到这一点,但如果没有包含 wordInTitle 的书则不行。有人可以帮我吗?
public String[] getBookTitlesContaining(String wordInTitle)
{
int nBooks = 0;
String[] bookTitle = new String[nBooks];
for (Book booksTitles : inventory)
{
if(booksTitles.getTitle().toLowerCase().contains(wordInTitle.toLowerCase()))
{
nBooks++;
}
}
int titleB = 0;
bookTitle = new String[nBooks];
for (Book booksTitles : inventory)
{
if(booksTitles.getTitle().toLowerCase().contains(wordInTitle.toLowerCase()))
{
bookTitle [titleB] = booksTitles.getTitle();
titleB++;
}
}
if ((wordInTitle == null) || (wordInTitle == ""))
{
return null;
}
return bookTitle;
提前致谢
统计有书名的书籍数量后,如果计数为0,则可以return null;
public String[] getBookTitlesContaining(String wordInTitle)
{
int nBooks = 0;
String[] bookTitle = new String[nBooks];
for (Book booksTitles : inventory)
{
if(booksTitles.getTitle().toLowerCase().contains(wordInTitle.toLowerCase()))
{
nBooks++;
}
}
if (nBooks == 0) return null;
更改此 if
以包括没有匹配图书的情况:
if ((wordInTitle == null) || (wordInTitle == "") || (nBooks == 0))
{
return null;
}
我有此代码,如果没有标题包含 wordInTitle 的书,或者如果 wordInTitle 为 null 或“”,我想 return null。我可以设法为 null 和 "" 做到这一点,但如果没有包含 wordInTitle 的书则不行。有人可以帮我吗?
public String[] getBookTitlesContaining(String wordInTitle)
{
int nBooks = 0;
String[] bookTitle = new String[nBooks];
for (Book booksTitles : inventory)
{
if(booksTitles.getTitle().toLowerCase().contains(wordInTitle.toLowerCase()))
{
nBooks++;
}
}
int titleB = 0;
bookTitle = new String[nBooks];
for (Book booksTitles : inventory)
{
if(booksTitles.getTitle().toLowerCase().contains(wordInTitle.toLowerCase()))
{
bookTitle [titleB] = booksTitles.getTitle();
titleB++;
}
}
if ((wordInTitle == null) || (wordInTitle == ""))
{
return null;
}
return bookTitle;
提前致谢
统计有书名的书籍数量后,如果计数为0,则可以return null;
public String[] getBookTitlesContaining(String wordInTitle)
{
int nBooks = 0;
String[] bookTitle = new String[nBooks];
for (Book booksTitles : inventory)
{
if(booksTitles.getTitle().toLowerCase().contains(wordInTitle.toLowerCase()))
{
nBooks++;
}
}
if (nBooks == 0) return null;
更改此 if
以包括没有匹配图书的情况:
if ((wordInTitle == null) || (wordInTitle == "") || (nBooks == 0))
{
return null;
}