我有一個字符串和單詞列表。我想搜索該字符串中的每個單詞,並用UPDATE DATABASE到UPD DB等任何快捷方式替換該單詞。 下面是的話,我需要尋找搜索關鍵字並用它們的縮寫替換它們
ALTER DATABASE,ALTER TABLE,ALTER VIEW,CREATE DATABASE,CREATE PROCEDURE,CREATE SCHEMA,CREATE TABLE,CREATE VIEW,DELETE FROM,DROP DATABASE,DROP PROCEDURE,DROP名單TABLE,DROP VIEW,UPDATE DATABASE。
這是我的代碼===>
if(payloadStr.contains("UPDATE DATABASE")){
payloadStr = payloadStr.replace("UPDATE DATABASE","UPD DB");
} else if(payloadStr.contains("ALTER DATABASE")) {
payloadStr = payloadStr.replace("ALTER DATABASE", "ALTR DB");
}
的if else if條件我用,但我不認爲這是一個有效的方式來編碼。任何人都可以幫助我解決這個問題。我問過我的朋友他們告訴我使用REGEX,但我覺得正則表達式對我來說很複雜。
下面是我的情況了答案:
String[] words = { "UPDATE DATABASE", "ALTER DATABASE",
"ALTER TABLE", "ALTER VIEW", "CREATE DATABASE",
"CREATE PROCEDURE", "CREATE SCHEMA", "CREATE TABLE",
"CREATE VIEW", "DELETE FROM", "DROP DATABASE",
"DROP PROCEDURE", "DROP TABLE", "DROP VIEW" };
String[] replaceWith = { "UPD DB", "ALTR DB", "ALTR TBL",
"ALTR VW", "CRT DB", "CRT PRCR", "CRT SCHM", "CRT TBL",
"CRT VW", "DEL FRM", "DRP DB", "DROP PRCR", "DRP TBL",
"DRP VW" };
for (int i = 0; i < words.length; i++) {
payloadStr = payloadStr.replaceAll(words[i], replaceWith[i]);
getLogger().debug(
"SQL Statement in Message. Message Modified To Avoid Layer-7 Rejection: "
+ payloadStr);
}
_I使用,如果其他人如果condition_然後告訴我們。 – Smit 2013-05-02 18:58:58
*有些人遇到問題時,認爲 「我知道,我會用正則表達式。」現在他們有兩個問題。* - Jamie Zawinski – 2013-05-02 18:59:52
這是我的代碼===> if(payloadStr.contains (「UPDATE DATABASE」)){payloadStr = payloadStr.replace(「UPDATE DATABASE」,「UPD DB」); \t \t }否則如果(payloadStr.contains( 「ALTER DATABASE」)){ \t \t payloadStr = payloadStr.replace( 「ALTER DATABASE」, 「ALTR DB」); \t} – user1771540 2013-05-02 19:10:59