java - Censored Words Condition -


i need program print "censored" if userinput contains word "darn", else print userinput, ending newline.

i have:

import java.util.scanner;  public class censoredwords {     public static void main (string [] args) {         string userinput = "";         scanner scan = new scanner(system.in);         userinput = scan.nextline;          if(){             system.out.print("censored");         }             else{             system.out.print(userinput);         }          return;     } } 

not sure condition if can be, don't think there "contains" method in string class.

the best solution use regex word boundary.

if(mystring.matches(".*?\\bdarn\\b.*?"))

this prevents matching sdarnsas rude word. :) demo here


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -