How to get ASCII for non-numeric characters in a given string

I will have my line as follows

String S="AB-1233-444"; 

From this, I would like to single out AB and would like to learn ASCII for this 2 alphabets.

+4
source share
5 answers

You should be able to use LINQ to take care of this (syntax testing now):

 var asciiCodes = S.Where(c => char.IsLetter(c)).Select(c => (int)c); 

Or if you do not want to use the LINQ-y version:

 var characterCodes = new List<int>(); foreach(var c in S) { if(char.IsLetter(c)) { characterCodes.Add((int)c); } } 
+1
source

You can convert a character to code using this: (int)'a' .

0
source

To separate (if you know that it is split, you can use string.Split

To get an ASCII representation of "A", for example, use the following code

 int asciivalue = (int)'A'; 

Such a complete example could be

 Dictionary<char,int> asciilist = new Dictionary<char,int>(); string s = "AB-1233-444"; string[] splitstrings = s.Split('-'); foreach( char c in splitstrings[0]){ asciilist.Add( c, (int)c ); } 
0
source
  var result = (from c in S.ToCharArray() where ((int)c >= (int)'a' && (int)c <= (int)'z') || ((int)c >= (int)'A' && (int)c <= (int)'Z') select c).ToArray(); 

The non-linear version is as follows:

 List<char> result = new List<char>(); foreach(char c in S) { if(((int)c >= (int)'a' && (int)c <= (int)'z') || ((int)c >= (int)'A' && (int)c <= (int)'Z')) { result.Add(c); } } 
0
source

You can use a substring to get the alphabets alone and use the for loop to store the values ​​of the alphabets in an array and print one after the other

0
source

All Articles