JAVA: check a string if there is a special character in it

0 votes
asked Nov 25, 2009 by blue

How do you check a string if there is a special character like: [,],{,},{,),*,|,:,>,etc.?

13 Answers

0 votes
answered Nov 25, 2009 by michael-zilbermann

What do you exactly call "special character" ? If you mean something like "anything that is not alphanumeric" you can use org.apache.commons.lang.StringUtils class (methods IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable).

If it is not so trivial, you can use a regex that defines the exact character list you accept and match the string against it.

0 votes
answered Nov 25, 2009 by djna

All depends on exactly what you mean by "special". In a regex you can specify

  • \W to mean non-alpahnumeric
  • \p{Punct} to mean punctuation characters

I suspect that the latter is what you mean. But if not use a [] list to specify exactly what you want.

0 votes
answered Nov 25, 2009 by andreas-d

Have a look at the java.lang.Character class. It has some test methods and you may find one that fits your needs.

Examples: Character.isSpaceChar(c) or !Character.isJavaLetter(c)

0 votes
answered Nov 25, 2009 by will

Visit each character in the string to see if that character is in a blacklist of special characters; this is O(n*m).

The pseudo-code is:

for each char in string:
  if char in blacklist:
    ...

The complexity can be slightly improved by sorting the blacklist so that you can early-exit each check. However, the string find function is probably native code, so this optimisation - which would be in Java byte-code - could well be slower.

0 votes
answered Nov 25, 2009 by r3zn1k
Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("I am a string");
boolean b = m.find();

if (b)
   System.out.println("There is a special character in my string");
0 votes
answered Nov 25, 2009 by varun

First you have to exhaustively identify the special characters that you want to check.

Then you can write a regular expression and use

public boolean matches(String regex)
0 votes
answered Nov 16, 2011 by bhushan
Pattern p = Pattern.compile("[\\p{Alpha}]*[\\p{Punct}][\\p{Alpha}]*");
        Matcher m = p.matcher("Afsff%esfsf098");
        boolean b = m.matches();

        if (b == true)
           System.out.println("There is a sp. character in my string");
        else
            System.out.println("There is no sp. char.");
0 votes
answered Nov 16, 2011 by fastcodejava

If it matches regex [a-zA-Z0-9 ]* then there is not special characters in it.

0 votes
answered Nov 26, 2014 by ashish-sharma

You can use the following code to detect special character from string.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DetectSpecial{ 
public int getSpecialCharacterCount(String s) {
     if (s == null || s.trim().isEmpty()) {
         System.out.println("Incorrect format of string");
         return 0;
     }
     Pattern p = Pattern.compile("[^A-Za-z0-9]");
     Matcher m = p.matcher(s);
    // boolean b = m.matches();
     boolean b = m.find();
     if (b == true)
        System.out.println("There is a special character in my string ");
     else
         System.out.println("There is no special char.");
     return 0;
 }
}
0 votes
answered Sep 15, 2017 by antroid

this worked for me

    String s = "string"; 
    if(Pattern.matches("[a-zA-Z]+", s)) 
    {  
    System.out.println("clear"); 
     }  
     else  
     {  
     System.out.println("buzz"); 
     }  
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...