aspose file tools*
The moose likes Java in General and the fly likes how to remove strange control characters from a string    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to remove strange control characters from a string    " Watch "how to remove strange control characters from a string    " New topic
Author

how to remove strange control characters from a string

Jack Wanes
Greenhorn

Joined: Sep 18, 2006
Posts: 22
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
7F is "delete." How on earth did that get there?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
BTW: Wasn't this question on the Swing forum earlier?
Srikanth Ramu
Ranch Hand

Joined: Feb 20, 2007
Posts: 76
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

Joined: May 06, 2004
Posts: 262
He never revisited the two threads he started on this topic at the Sun Java forums, either.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to remove strange control characters from a string