
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Get first three letters from every string in C#
The following are our strings in a list −
List<object> list = new List<object> { "keyboard", "mouse", "joystick", "monitor" };
To use the first 3 letters, use substring method and use it under the Linq Select method.
IEnumerable<string> res = list.AsQueryable() .Cast<string>() .Select(str => str.Substring(0, 3));
Example
using System; using System.Linq; using System.Collections.Generic; class Demo { static void Main() { List<object> list = new List<object> { "keyboard", "mouse", "joystick", "monitor" }; // getting first 3 letters from every string IEnumerable<string> res = list.AsQueryable() .Cast<string>() .Select(str => str.Substring(0,3)); foreach (string str in res) { Console.WriteLine(str); } } }
Output
key mou joy mon
- Related Articles
- JavaScript Insert space after every two letters in string?
- Interchanging first letters of words in a string in JavaScript
- C# Program to get the first three elements from a list
- Get distinct first word from a string with MongoDB?
- Python – Get Every Element from a String List except for a specified letter
- How to get first day of every corresponding month in MySQL?
- Styling First-Letters with CSS ::first-letter
- Finding the power of a string from a string with repeated letters in JavaScript
- Python program to find top three mostly occurred letters from company name
- How to get the maximum count of repeated letters in a string? JavaScript
- Python Program that Displays which Letters are in the First String but not in the Second
- Get first key from NavigableMap in Java
- Get first entry from NavigableMap in Java
- MySQL query to get a substring from a string except the last three characters?
- Get distinct first words in a string with MongoDB?

Advertisements