以下方法用空格替換字符串變量中的每個括號和每個逗號。它還使用正則表達式僅用一個空格替換多個空格。在我的簡單代碼中找不到邏輯錯誤
// -----------------------------------------------------
// Replace special characters by spaces.
// -----------------------------------------------------
private String ParseCommand(String Command)
{
String strCommand = Command;
// Replace parenthesis and commas by a space.
strCommand = strCommand.replace('(', ' ');
strCommand = strCommand.replace(')', ' ');
strCommand = strCommand.replace(',', ' ');
// Remove extra spaces.
strCommand = strCommand.replaceAll("\\s+"," ");
return strCommand;
}
上述方法「ParseCommand」由方法「SplitAndFind」調用,該方法根據空間拆分字符串。此外,搜索結果數組
// -----------------------------------------------------
// Find a token in command.
// -----------------------------------------------------
public void SplitAndFind(String Command, String TokenToFind)
{
String strCommand = ParseCommand(Command);
String[] strTokens = strCommand.split(" ");
for (int i = 0; i <= strTokens.length - 1; i++)
{
System.out.println(strTokens[i]);
if (strTokens[i] == TokenToFind)
{
System.out.println("TOKEN FOUND !!!");
}
}
}
最後,我尋找的令牌PRIMARY
調用方法SplitAndFind
從main
在令牌。我的問題是沒有找到令牌。我在標記數組中顯示每個項目,我看到它但是「TOKEN FOUND !!!」消息從不顯示。我究竟做錯了什麼?
public static void main(String[] args) throws FileNotFoundException, IOException
{
dbEngine objEngine = new dbEngine();
objEngine.SplitAndFind("CREATE TABLE animals (PRIMARY VARCHAR(20), kind VARCHAR(8), years INTEGER) PRIMARY KEY (name, kind);", "PRIMARY");
}
它'如果(strTokens [I] .equals(TokenToFind))' – Satya