Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a String contains a special character

How do you check if a String contains a special character like:

[,],{,},{,),*,|,:,>, 
like image 374
blue Avatar asked Nov 25 '09 08:11

blue


People also ask

How do you check if there is a special character in a string Python?

Approach : Make a regular expression(regex) object of all the special characters that we don't want, then pass a string in search method. If any one character of string is matching with regex object then search method returns a match object otherwise return None.

How do you check if a string contains a specific character in JavaScript?

You can check if a JavaScript string contains a character or phrase using the includes() method, indexOf(), or a regular expression. includes() is the most common method for checking if a string contains a letter or series of letters, and was designed specifically for that purpose.

How do I find special characters?

Click Start, point to Settings, click Control Panel, and then click Add/Remove Programs. Click the Windows Setup tab. Click System Tools (click the words, not the check box), and then click Details. Click to select the Character Map check box, click OK, and then click OK.


2 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"); 
like image 72
oliver31 Avatar answered Oct 30 '22 00:10

oliver31


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)         System.out.println("There is a special character in my string ");      else          System.out.println("There is no special char.");      return 0;  } } 
like image 27
Ashish Sharma Avatar answered Oct 30 '22 00:10

Ashish Sharma