Replace with a regular expression:
Code snippet:
String documentTxt = EntityUtils. toString (entity, "gbk"); // get data
DocumentTxt = documentTxt. replaceAll ("[\ t \ n \ r]", ""); // remove the line breaks in the content area
Note: replaceAll of the String class provides the regular expression replacement function. \ T is a Tab character \ n is a line break \ r is a carriage return
Java Regular Expression usage:
Example method:
Copy codeThe Code is as follows: public void parseTxt (String content ){
Pattern p = Pattern. compile (Config. articlePtn );
Matcher matcher = p. matcher (content );
While (matcher. find ()){
System. out. println (matcher. group (1 ));
}
}
Note: you only need to remember the Pattern class. Its static method complie parses a regular expression to generate a Pattern object.
Then use the model to match the string, get a Matcher, and use the find method of matcher to traverse all the matches.
Group is the group in the Regular Expression and the () expression. Group (0) is the original string, and gourp (1) is the first matched group... that is, the index of the matched group starts from 1.