java - Split String on \b's but not on \b's between a substring -


how split string words leave phrases/terms intact? right now, have string[] strarr = str.split("\\b");, want modify regex parameter accomplished mentioned above. solution doesn't have include regex

for example, if str equals "the city of san francisco beautiful!" , term "san francisco", how split str resulting string[] array looks such: ["the", "city", "of", "san francisco", "is", "truly", "beautiful!"]?


after seeing @radiodef's comment, decided don't require regex per se. if can me solve problem, still appreciated!

well that's interesting question. approach write general method in detecting number of word phrases returning simple array of strings.

here demo

below method,

 string[] find(string m[], string c[], string catchstr){      string comp = c[0];     arraylist<string> list = new arraylist<string>();     for(int i=0;i<m.length;i++){          boolean flag = false;          //comparing if substring matches or not         if(comp.equals(m[i])){             flag = true;             for(int j=0;j<c.length;j++){                 //you can use equalsignorecase() if want compare string                  //ignoring case                 if(!m[i+j].equals(c[j])){                     flag = false;                     break;                 }             }          }          if(flag){             list.add(catchstr);             = + c.length-1;         }else{             list.add(m[i]);         }      }      //converting result string array     string finalarr[] = list.toarray(new string[list.size()]);      return finalarr;  } 

you can call function as,

string mainstr = "the city of san francisco beautiful!"; string catchstr = "san francisco"; string mainstrarr[] = mainstr.split(" "); string catchstrarr[] = catchstr.split(" ");  string finalarr[] = find(mainstrarr, catchstrarr, catchstr); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -