C#: How to Query for characters in a string using LinQ.

 C#     sbo     Updated on Aug 31, 2013     VIKRAM     510   

Introduction:

In this article i will explain how to query for characters in a string using LinQ in C#

Description:

In previous articles i explained How to replace string in C#How to convert string to int in C#, and How to read characters from string. In this article i will explain how to query for characters in a string using LinQ in C#.

Example:

string Email = "vikram";

Now read all characters from Email string by using LinQ.

// insert all characters into Array.
var characters = from c in Email
                 select c;

Now display each character from characters array in new line by using foreach.

foreach (char c in characters)
{
    // Display character by character.
    Console.WriteLine(" " + c + "\n");
}

Full Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpPractice
{
    class QueryAsString
    {
        public static void Main()
        {
            string Email = "vikram";
            // insert all characters into  Array.
            var characters = from c in Email
                             select c;
            Console.WriteLine(" Characters: ");
            foreach (char c in characters)
            {
                // Display character by character.
                Console.WriteLine(" " + c + "\n");
            }
            Console.ReadLine();
        }
    }
}

Output:

  Related Articles
The major features added in C# in each release
C# Coding Standards and Naming Conventions
ASP.Net: ListView Basics - ItemEditing, ItemUpdating and ItemCanceling events
C#: How to Read characters from String
Access Specifiers or Access Modifiers in C#
  Recent Articles
The major features added in C# in each release
C# Coding Standards and Naming Conventions
ASP.Net: ListView Basics - ItemEditing, ItemUpdating and ItemCanceling events
C#: How to Read characters from String
Access Specifiers or Access Modifiers in C#