java - Search for keywords and replace them with their abbreviations -


i have string , list of words. want search each word in string , replace word shortcut update database upd db. below list of words need

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.

this code ===>

   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");  } 

i used if else if condition don't think efficient way code. can 1 please me in issue. asked friends told me use regex feel regex complex me understand.

below answer case:

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" };              (int = 0; < words.length; i++) {                  payloadstr = payloadstr.replaceall(words[i], replacewith[i]);                 getlogger().debug(                         "sql statement in message. message modified avoid layer-7 rejection:  "                                 + payloadstr);             } 

make sure use replaceall(). put of terms , replacements in array.

like so:

string text = "alter database, alter table, alter view, create database, create procedure, create schema, create table";  string[] terms = {alter database, alter table, alter view, create database, create procedure, create schema, create table};  string[] replacewith = {"","","","","","",""}; // whatever replace  for(int = 0; < terms; i++) {     // may want account uppercase/lower case here     text = text.replaceall(terms[i],replacewith[i]); } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -