How do you check if a String contains a special character like:
[,],{,},{,),*,|,:,>,
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.
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.
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.
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");
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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With