Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get first three letters from every string in C#
In C#, you can extract the first three letters from every string using the Substring() method combined with LINQ operations. This is useful for creating abbreviations, prefixes, or shortened versions of strings in a collection.
Syntax
Following is the syntax for getting a substring from a string −
string.Substring(startIndex, length)
Following is the syntax for applying substring to a collection using LINQ −
collection.Select(str => str.Substring(0, 3))
Using Substring with LINQ Select
The most common approach is to use the Substring() method with LINQ's Select() method to transform each string in the collection −
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<string> list = new List<string> { "keyboard", "mouse", "joystick", "monitor" };
// getting first 3 letters from every string
IEnumerable<string> res = list.Select(str => str.Substring(0, 3));
foreach (string str in res) {
Console.WriteLine(str);
}
}
}
The output of the above code is −
key mou joy mon
Using Safe Substring with Error Handling
To handle strings shorter than 3 characters, you can add length checking to avoid exceptions −
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<string> list = new List<string> { "keyboard", "hi", "joystick", "a", "monitor" };
// safe substring - handles strings shorter than 3 characters
IEnumerable<string> res = list.Select(str =>
str.Length >= 3 ? str.Substring(0, 3) : str);
foreach (string str in res) {
Console.WriteLine("'" + str + "'");
}
}
}
The output of the above code is −
'key' 'hi' 'joy' 'a' 'mon'
Using Take Method Alternative
Another approach is to use LINQ's Take() method to get the first 3 characters −
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<string> list = new List<string> { "keyboard", "mouse", "joystick", "monitor" };
// using Take to get first 3 characters
IEnumerable<string> res = list.Select(str => new string(str.Take(3).ToArray()));
foreach (string str in res) {
Console.WriteLine(str);
}
}
}
The output of the above code is −
key mou joy mon
Comparison of Approaches
| Method | Performance | Error Handling |
|---|---|---|
| Substring(0, 3) | Fastest | Throws exception if string length < 3 |
| Safe Substring with length check | Fast | Handles short strings gracefully |
| Take(3) method | Slower | Automatically handles short strings |
Conclusion
The Substring(0, 3) method combined with LINQ Select() is the most efficient way to extract the first three letters from strings in a collection. For safer code that handles varying string lengths, add length validation or use the Take() method alternative.
