0

i have a string and i need a utility method which looks for bad character presence in the string and return some flag on that. Thanks for help in advance

Karn_way
  • 1,005
  • 3
  • 19
  • 42
  • 5
    What do you mean by "bad character" and [what have you tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/) so far? – Pshemo Jul 03 '12 at 10:57
  • 1
    See http://stackoverflow.com/questions/1184176/how-can-i-safely-encode-a-string-in-java-to-use-as-a-filename and especially the answer http://stackoverflow.com/a/1184180/814206 about *replacing* (instead of flagging) all 'bad' characters with other safe characters. – Kasper van den Berg Jul 03 '12 at 11:02
  • ï ¿ ½r these characters i want to skip . i am not sure what others may so asked in a generic way – Karn_way Jul 03 '12 at 11:30

2 Answers2

1

It isn't clear exactly what you are trying to do from the question, but you can probably just use String.indexOf(...) to detect the presence of the "bad character"

mikera
  • 105,238
  • 25
  • 256
  • 415
  • 1
    ï ¿ ½r these are something that cannot be input from keyboard so i thought might there must be some API which filters such characters – Karn_way Jul 03 '12 at 11:31
1

Use regular expressions to define what characters do you want to appear in a string. If something else appears, report it somehow.

Simple example:

String s = "humbapumpa jim";
if (!s.matches("[a-zA-Z]+")) return false;

Or from the other side: define your "bad" characters and fire alert if they appear.

String s = "humbapumpa jim";
if (s.matches("[a]+")) return false;

More info on regular expressions in Java: http://www.vogella.com/articles/JavaRegularExpressions/article.html

Alexander
  • 1,299
  • 2
  • 12
  • 32