using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class TestIO
{
static void Main()
{
string line = "first-main-in-c#";
var stringQuery = from ch in line where Char.IsDigit(ch) select new string(ch + '-');
foreach (var c in stringQuery)
Console.Write(c);
Console.WriteLine(System.Environment.NewLine + "Press any key to exit");
Console.ReadKey();
}
}
}
I am beginner in c# , what is my mistake, I expected this output:
f-i-r-s-t-m-a-i-n-i-n-c
but i get :
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Char,<>f__Anonymou
sType0`1[System.Int32]]
Char.IsDigit determines whether the character is a decimal digit. There are no decimal digit characters in that string. You could use
Char.IsLetterorChar.IsLetterOrDigit, depending on you want to include digits or not.You are also calling a constructor on string that does not exist. You could use the one that takes a char array.
Finally, the query will return a sequence of strings. To get the desired result (one string as you stated), the code taking the above into account, could be:
This however will include a – at the end you will need to trim off.
String.Joinjoins strings with a separator, so to get the result you want, this could be simpler: