The following method replaces each parenthesis and each comma in a string variable with a space. It also replaces multiple spaces with only one space using a regular expression.
// -----------------------------------------------------
// 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;
}
Above method "ParseCommand" is called by method "SplitAndFind" which splits a string based on a space. Also, it searches for a token in the resulting array
// -----------------------------------------------------
// 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 !!!");
}
}
}
Finally I call method SplitAndFind
from main
looking for the token PRIMARY
. My problem is that the token is not found. I display every item in the array of tokens and I see it but the message "TOKEN FOUND !!!" is never displayed. What am I doing wrong?
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");
}