यह जांचने के लिए कि क्या किसी स्ट्रिंग में कोई विशेष वर्ण है, आपको निम्न विधि का उपयोग करने की आवश्यकता है -
Char.IsLetterOrDigit
लूप और चेक या विशेष वर्ण वाले स्ट्रिंग के लिए इसे अंदर उपयोग करें।
मान लें कि हमारा स्ट्रिंग है -
string str = "Amit$#%";
अब स्ट्रिंग को कैरेक्टर ऐरे में बदलें -
str.ToCharArray();
इसके साथ, लूप के लिए उपयोग करें और 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: $ # %