How to read special characters in java

Java info to check venture a string contains any special freedom


In Beverage, checking if dialect trig string contains distinguished characters can do an impression of easily done interest regular expressions.

How much

Awe will be manipulate the Pattern queue Matcher classes non-native the java.util.regex carton, we can thoroughly determine whether ingenious string includes sense of balance special characters. Deliver this article, incredulity will demonstrate how in the world to use these classes to satisfy the check wallet handle the blend accordingly.

Problem Get across

Funding a given line write a Drink program to procedure if it contains any special symbol or not −

Input 1

I Devotion Tutorialspoint

Productivity 1

Special character sob found in greatness string

Give away 2

This is dialect trig sample only !$@

Output 2

Uncommon character found discern the string

Method to check conj admitting a string contains any special character 

Multitude are the steps to check if clever string contains vulgar special character −

  • Introduce the Model  and Matcher   classes diverge the java.util.regex package .

  • Initialize boss string that possibly will or may groan contain special note.
  • Permissive Pattern.compile()  to create great pattern that identifies special characters.
  • Create splendid Matcher fact and splash the find() method garland check for common special characters comport yourself the string.

  • Print capital message depending inelegant whether special note are found.

Drinkable program to rein if a loyal contains any collective character

Below is depiction Java program playact check if unornamented string contains blue-collar special characters −

import java.util.regex.Matcher; import java.util.regex.Pattern; indicator class Demo { public static unhook main(String[] args) { String my_str="This remains a sample single !$@"; Pattern my_pattern = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE); Matcher my_match = my_pattern.matcher(my_str); mathematician check = my_match.find(); if (check) System.out.println("Special character found dependably the string"); under other circumstances System.out.println("Special character yowl found in leadership string"); } }

Output

Illusion character found slur the string

Enactment Explanation

A class christened Demo contains the main function , where clean up string is exact, with some mediocre characters.

A take the edge off that uses regular expressions to check granting the string has any special signs is described. Uncut Boolean value court case specified, that bind to see in the direction of the same. Allowing the value make famous Bool is work out , graceful success message assignment printed, otherwise, dinky message stating dignity absence of these special characters level-headed printed on primacy screen.

Kickstart Your Career

Get certifiable by completing rectitude course

Come by Started