DEV Community

Cover image for Check if a string contains special characters java
joshua-brown0010
joshua-brown0010

Posted on

Check if a string contains special characters java

In this article, you will learn how to check special characters present in a string. I am going to explain simply about the string class methods used to check special characters without using regex.

Approach

  • In order to check the non- alphanumeric characters in any given string, I am using a basic approach without any regex class.
  • In the example, I have taken a demo input with some special characters in it to make you understand that my code works well.
  • I have also taken the specialcharacters in a string because we need to find the special characters in a string.
  • By using for loop I have cross-checked each and every character in the given string with all the specialcharcaters string.
  • If it contains a special character, then it returns an output statement that the given string has a special character in it.
  • Else it checks for the condition whether the character index value equals to the total string length, then it indicates zero special characters in the string. Read more

Top comments (0)