Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to remove strange control characters from a string

 
Jack Wanes
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using some legacy swing application, and every time I use ctrl-v or ctrl-c, there is a strange control character generated (it looks like a square/rectangle) at the end of the string. I cannot paste this special character here while I can in Eclipse IDE, so I just used its unicode representation. The problem is, if I replace \u007F with this special character, I cannot use replaceAll("[\u007F]","") to remove it from the string, while I can remove it if the string contains unicode representation such as
"first\u007F". I am wondering how I can remove it from string when it does not have unicode representation. Thanks.

public class ControlCharacterTest {
public static void main(String[] args){
char controlChar = '\u007F';
String cChar = new Character(controlChar).toString();
System.out.println("Control char == " + cChar);

String input = "first\u007F";
String newInput = input.replaceAll ("[\u007F]","");
System.out.println("newInput == " + newInput);

String newInput2 = input.replaceAll ("[\u0000-\u0020]","");
System.out.println("newInput2 == " + newInput2);
}
}

[ April 05, 2007: Message edited by: Jack Wanes ]
[ April 05, 2007: Message edited by: Jack Wanes ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
7F is "delete." How on earth did that get there?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW: Wasn't this question on the Swing forum earlier?
 
Srikanth Ramu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to remove unwanted characters you may use toCharArray() and iterate each character and check if they are valid one. You could use str.matches("\\w") to check the validity.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He never revisited the two threads he started on this topic at the Sun Java forums, either.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic