Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

문자열에 특수 문자가 포함되어 있는지 확인하는 C# 프로그램


문자열에 특수 문자가 포함되어 있는지 확인하려면 다음 방법을 사용해야 합니다. -

Char.IsLetterOrDigit

for 루프 및 검사 또는 특수 문자가 포함된 문자열 내부에서 사용합니다.

문자열이 −

라고 가정해 보겠습니다.
string str = "Amit$#%";

이제 문자열을 문자 배열로 변환 -

str.ToCharArray();

이와 함께 for 루프를 사용하고 isLetterOrDigit() 메서드를 사용하여 각 문자를 확인합니다.

예시

전체 코드를 살펴보겠습니다.

using System;
namespace Demo {
   class myApplication {
      static void Main(string[] args) {
         string str = "Amit$#%";
         char[] one = str.ToCharArray();
         char[] two = new char[one.Length];
         int c = 0;
         for (int i = 0; i < one.Length; i++) {
            if (!Char.IsLetterOrDigit(one[i])) {
               two[c] = one[i];
               c++;
            }
         }
         Array.Resize(ref two, c);
         Console.WriteLine("Following are the special characters:");
         foreach(var items in two) {
            Console.WriteLine(items);
         }
         Console.ReadLine();
      }
   }
}

출력

Following are the special characters:
$
#
%