Check if string contains special character

How to check if a string contains a special character, such as:

[,],{,},{,),*,|,:,>, 
+66
java string special-characters
Nov 25 '09 at 8:15
source share
16 answers
 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"); 
+114
Nov 25 '09 at 8:25
source share

You can use the following code to detect a special character from a 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) System.out.println("There is a special character in my string "); else System.out.println("There is no special char."); return 0; } } 
+21
Jun 26 '14 at 8:17
source share

If it matches the regular expression [a-zA-Z0-9 ]* , then there are no special characters.

+14
Jun 16 '11 at 1:45 a.m.
source share

If you want to have LETTERS, SPECIAL CHARACTERS and NUMBERS in your password with at least 8 digits, then use this code, it works fine

 public static boolean Password_Validation(String password) { if(password.length()>=8) { Pattern letter = Pattern.compile("[a-zA-z]"); Pattern digit = Pattern.compile("[0-9]"); Pattern special = Pattern.compile ("[!@#$%&*()_+=|<>?{}\\[\\]~-]"); //Pattern eight = Pattern.compile (".{8}"); Matcher hasLetter = letter.matcher(password); Matcher hasDigit = digit.matcher(password); Matcher hasSpecial = special.matcher(password); return hasLetter.find() && hasDigit.find() && hasSpecial.find(); } else return false; } 
+10
Jan 17 '17 at 12:49 on
source share

What do you call a "special character"? If you mean something like β€œnothing that is alphanumeric,” you can use the org.apache.commons.lang.StringUtils class (IsAlpha / IsNumeric / IsWhitespace / IsAsciiPrintable methods).

If this is not so trivial, you can use a regular expression that defines the exact list of characters you accept and match the string with it.

+9
Nov 25 '09 at 8:18
source share

It all depends on what you mean by "special." In regex you can specify

  • \ W means non-alphanumeric
  • \ p {Punct} means punctuation

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

+7
Nov 25 '09 at 8:20
source share

Take a look at the java.lang.Character class. It has several testing methods, and you can find one that suits your needs.

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

+7
Nov 25 '09 at 8:22
source share

You must first comprehensively identify the special characters that you want to test.

Then you can write a regular expression and use

 public boolean matches(String regex) 
+3
Nov 25 '09 at 8:25
source share

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

Pseudocode:

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

Difficulty can be slightly improved by sorting the blacklist so that you can exit each check early. However, the string search function is probably native code, so this optimization, which will be in Java bytecode, can be slower.

+1
Nov 25 '09 at 8:24
source share
 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."); 
+1
Jun 16 2018-11-11T00:
source share

// without using a regular expression ........

  String specialCharacters=" !#$%&'()*+,-./:;<=>?@[]^_`{|}~0123456789"; String name="3_ saroj@"; String str2[]=name.split(""); for (int i=0;i<str2.length;i++) { if (specialCharacters.contains(str2[i])) { System.out.println("true"); //break; } else System.out.println("false"); } 
+1
Feb 22 '17 at 19:22
source share

// this is an updated version of the code I posted / * the isValidName method checks to see if the name passed as an argument should be 1. invalid value or space 2. special character 3.Digits (0-9) Explanation --- Here str2 - this is a String array variable that stores a split name string, which is passed as an argument. The variable count counts the number of special characters. The method will return true if it satisfies all conditions * /

 public boolean isValidName(String name) { String specialCharacters=" !#$%&'()*+,-./:;<=>?@[]^_`{|}~0123456789"; String str2[]=name.split(""); int count=0; for (int i=0;i<str2.length;i++) { if (specialCharacters.contains(str2[i])) { count++; } } if (name!=null && count==0 ) { return true; } else { return false; } } 
+1
Feb 23 '17 at 6:36
source share

in the string String str2 [] = name.split (""); give an extra character in the array ... Let me explain with an example "Aditya" .split ("") will return [, A, d, i, t, y, a] You will have an extra character in your array ...
"Aditya" .split ("") does not work as expected, using the saroj route, you will get an extra character in String => [, A, d, i, t, y, a].

I changed it, see below the code works as expected

  public static boolean isValidName(String inputString) { String specialCharacters = " !#$%&'()*+,-./:;<=>?@[]^_`{|}~0123456789"; String[] strlCharactersArray = new String[inputString.length()]; for (int i = 0; i < inputString.length(); i++) { strlCharactersArray[i] = Character .toString(inputString.charAt(i)); } //now strlCharactersArray[i]=[A, d, i, t, y, a] int count = 0; for (int i = 0; i < strlCharactersArray.length; i++) { if (specialCharacters.contains( strlCharactersArray[i])) { count++; } } if (inputString != null && count == 0) { return true; } else { return false; } } 
+1
Apr 25 '17 at 10:46 on
source share

This worked for me:

 String s = "string"; if (Pattern.matches("[a-zA-Z]+", s)) { System.out.println("clear"); } else { System.out.println("buzz"); } 
+1
Sep 12 '17 at 19:05
source share

Convert the string to an array of characters with all lowercase letters:

 char c[] = str.toLowerCase().toCharArray(); 

You can then use Character.isLetterOrDigit(c[index]) to find out which index has special characters.

0
Oct 13 '18 at 21:54
source share

Use matches of the static method of the java.util.regex.Pattern class (regex, String obj)
regex: lowercase and uppercase characters and numbers 0 to 9
String obj: The String object you want to check if it contains a special character or not.

It returns a boolean true if it only contains characters and numbers, otherwise it returns a boolean false

Example.

 String isin = "12GBIU34RT12";<br> if(Pattern.matches("[a-zA-Z0-9]+", isin)<br>{<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Valid isin");<br> }else{<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Invalid isin");<br> } 
-one
Jan 17 '19 at 6:28
source share



All Articles