简体   繁体   中英

Remove carriage return and special characters from text

I am writing a code in java that takes text then remove all the Punctuations (, blanks - new line and numerals) all special character and just leave the A to Z letters. it works fine until gets to the first carriage return and stopped. I tried many version of replaceAll but it did not work, please help!

for example

IHN EMATMG EECNIWEA RSHI A RESOEF ES RLTCMEE-COEAACIROH TLNHR PIRMOA ECSHENEV CEDIAODED ULS NPHD TN EAE REIIY-MO TWL-EDTHTTEEN NTCIPRO TO TUERYMT MORCCIECLL,

PIMAATODMC DNL IITIAMRO CUNAIMYNAOINI.

then I get:

IHNEMATMGEECNIWEARSHIARESOEFESRLTCMEECOEAACIROHTLNHRPIRMOAECSHENEVCEDIAODEDULSNPHDTNEAEREIIYMOTWLEDTHTTEENNTCIPROTOTUERYMTMORCCIECLL

 package Cipher1;

import java.util.Scanner;

public class StripCipher
{
    public static void main(String[] args)
    {
        // Take the input of the encrypted text from the user.
        System.out.println(" Enter the cipher text : ");
        Scanner ScanText = new Scanner(System.in);
        String OriginalCipherText = ScanText.nextLine();

        // Eliminate the wide space and special characters present in the input
        // text.
        String CipherText = OriginalCipherText.replaceAll("\\s+", "");
        CipherText = OriginalCipherText.replaceAll("[^a-zA-Z]+", "");
        System.out.println(" Striped Cipher text is : " + CipherText);

        // Calculate the length of the text.
        int CipherTextLength = CipherText.length();
        System.out.println(" Lenght of the cipher text is : " + CipherTextLength);
    }
}

for clarifications I used the following but none of them work:

replaceAll("[\n\r]", ""); 
replaceAll("\\r|\\n", "")
replaceAll("[^\\w\\s]",""); 
replaceAll("[^\\p{L}\\p{Z}]","");

replaceAll returns a String with the replacements. The original string stays the same. Now, the problem is that you're having two replaceAll calls from the same original String and the second simply overwrites the changes from the first one:

String CipherText = OriginalCipherText.replaceAll("\\s+", "");
CipherText = OriginalCipherText.replaceAll("[^a-zA-Z]+", "");

You probably want

String CipherText = OriginalCipherText.replaceAll("\\s+", "");
CipherText = CipherText.replaceAll("[^a-zA-Z]+", "");

or a combined Regular Expression.

问题可能是ScanText.nextLine(),因为它将读取行直到检测到新行,因此在您的情况下,它将在MORCCIECLL之后停止采用字符串,我尝试提供硬编码的字符串,并且它的工作正常

Scanner.nextLine() scans up to the next line separator. What you will probably have to do is continue reading lines and join them together.

one Never ever start a variable name with a capital letter! Capital letters are for types ("FooBar") and for constants ("FOO_BAR"), a variable should be called like "fooBar":

static final FooBar FOO_BAR = new FooBar();
FooBar fooBar = new FooBar();

two I agree with Artjom B. String is an immutable class. Every method that at first might look like it is changing the original string actually returns a new string containing the changed data. This way you can even easily chain the method calls using fluent notation:

String cipherText = originalCipherText.replaceAll("\\s+", "")
                                      .replaceAll("[^a-zA-Z]+", "");

But then [^a-zA-Z] also contains \\s, so your solution can be reduced to a one liner:

String cipherText = originalCipherText.replaceAll("[^a-zA-Z]+", "");

three I also agree with J Earls, Jekin Kalriya on the Scanner being the root of your problems. If you want to read multiple lines, you need to iterate the Scanner's lines:

while (true) {
  final String cypherText = scanText.nextLine()
                                    .replaceAll("[^a-zA-Z]", "");
  System.out.println(cypherText);
}

Mind that in your scenario:

  1. this get's you into an infinite loop as the System.in never ends! You would to check for a certain input like to stop the iteration.
  2. The output comes after each line typed to the console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM