DEV Community

loading...
Cover image for Check if a string contains special characters java

Check if a string contains special characters java

joshuabrown0010 profile image joshua-brown0010 ・1 min read

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

Discussion (0)

pic
Editor guide