java - Remove carriage return and special characters from text -


i writing code in java takes text remove punctuations (, blanks - new line , numerals) special character , leave z letters. works fine until gets first carriage return , stopped. tried many version of replaceall did not work, please help!

for example

ihn ematmg eecniwea rshi resoef es rltcmee-coeaaciroh tlnhr pirmoa ecshenev cediaoded uls nphd tn eae reiiy-mo twl-edthtteen ntcipro tuerymt morcciecll,

pimaatodmc dnl iitiamro cunaimynaoini.

then get:

ihnematmgeecniwearshiaresoefesrltcmeecoeaacirohtlnhrpirmoaecshenevcediaodedulsnphdtneaereiiymotwledthtteenntciprototuerymtmorcciecll

 package cipher1;  import java.util.scanner;  public class stripcipher {     public static void main(string[] args)     {         // take input of encrypted text user.         system.out.println(" enter cipher text : ");         scanner scantext = new scanner(system.in);         string originalciphertext = scantext.nextline();          // eliminate wide space , special characters present in input         // text.         string ciphertext = originalciphertext.replaceall("\\s+", "");         ciphertext = originalciphertext.replaceall("[^a-za-z]+", "");         system.out.println(" striped cipher text : " + ciphertext);          // calculate length of text.         int ciphertextlength = ciphertext.length();         system.out.println(" lenght of cipher text : " + ciphertextlength);     } } 

for clarifications used following none of them work:

replaceall("[\n\r]", "");  replaceall("\\r|\\n", "") replaceall("[^\\w\\s]","");  replaceall("[^\\p{l}\\p{z}]",""); 

replaceall returns string replacements. original string stays same. now, problem you're having 2 replaceall calls same original string , second overwrites changes first one:

string ciphertext = originalciphertext.replaceall("\\s+", ""); ciphertext = originalciphertext.replaceall("[^a-za-z]+", ""); 

you want

string ciphertext = originalciphertext.replaceall("\\s+", ""); ciphertext = ciphertext.replaceall("[^a-za-z]+", ""); 

or combined regular expression.


Comments