Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

how to remove strange control characters from a string

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 74650
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
7F is "delete." How on earth did that get there?
 
Campbell Ritchie
Marshal
Posts: 74650
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW: Wasn't this question on the Swing forum earlier?
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He never revisited the two threads he started on this topic at the Sun Java forums, either.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic