java - How do i remove the large leading space in the output of this code for variable a -


i'm experimenting string , string replacement example in application strip bad language or malicious code characters strings . working output when compiled leaves large space prior output "vilification managed..."

//import java.util.regex.pattern; class grandaddy { public static void main (string [ ] args) {  //"now k's in place c used be" (now it's on)    string tempinvarate= "marduk"; string sminth= "subparticle"; string infiltrate= "cosmicwind"; tempinvarate=tempinvarate.replace ("k", "c"); string a= "piss shit !@£$%^* vilification managed"; string b=" mark scorfield"; a= a.replace("piss", ""); a= a.replace("shit", ""); a= a.replaceall("[^a-z-a-z0-9]", " "); a=a+b; system.out.println(tempinvarate);  system.out.println(tempinvarate.replace("k", "c")); system.out.println(sminth); system.out.println(sminth.replace("sub", "sminth")); system.out.println(infiltrate); system.out.println(infiltrate.replace("cosmic" , "minute")); //int n = a.length(); //pattern space = pattern.compile(" "); //string[] arr = space.split(a); //string[] splitstr = a.trim().split("\\s+"); system.out.println(a);  } } 

the leading spaces multiple reasons:

  • when remove expletives, surrounded spaces, not removed. capture expletives using following regex instead:

    \baaaa[^a-za-z]? 

    this regex capture word boundary, followed expletive (replaced aaaa here), , optional non-alphanumeric character. if want match aaaa inside word (e.g. xaaaa) remove word boundary \b.

  • your replacement of [^a-z-a-z0-9] replaces them space. can replace empty string instead:

    a= a.replaceall("[^a-z-a-z0-9]", ""); 

    however, notice all spaces stripped , vilificationmanaged... worked around allowing spaces (i use + capture , remove bunch of characters @ time, out of habit):

    a= a.replaceall("[^a-z-a-z0-9 ]+", ""); 

after these changes, string a quick brown fox jumps on aaaa lazy &^&% dog become a quick brown fox jumps on lazy dog.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -