Extract first few characters of a string in C#
This post will discuss how to extract the first few characters of a string in C#.
1. Using String.Substring() method
To extract the first n characters from the end of a string, you can use the String.Substring() method. The idea is to pass the starting index as 0 and the ending index as n. However, this would throw an ArgumentOutOfRangeException if the input string’s length is less than the number of characters to be removed. Therefore, you should ensure that the ending index is less than the string length.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; public static class StringExtensions { public static string Extract(this string input, int len) { if (string.IsNullOrEmpty(input) || input.Length < len) { return input; }; return input.Substring(0, len); } } public class Example { public static void Main() { string input = "Hello World"; string s = input.Extract(5); Console.WriteLine(s); // Hello } } |
2. Using C# 8 Ranges
Starting with C# 8, you can use the ranges .. to extract characters from the start of a string. It takes the start and end of a range as its operands. To avoid ArgumentOutOfRangeException when the input string’s length is less than the required length, you can use the Math.Min() method to determine the ending index.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; public static class StringExtensions { public static string Extract(this string input, int len) { if (string.IsNullOrEmpty(input)) { return input; } return input[0..Math.Min(input.Length, len)]; } } public class Example { public static void Main() { string input = "Hello World"; string s = input.Extract(5); Console.WriteLine(s); // Hello } } |
The above solution can be further shortened using the null-conditional operator ?. This eliminates the need for an explicit null check.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; public static class StringExtensions { public static string Extract(this string input, int len) { return input?[0..Math.Min(input.Length, len)]; } } public class Example { public static void Main() { string input = "Hello World"; string s = input.Extract(5); Console.WriteLine(s); // Hello } } |
3. Using Enumerable.Take() method
If you are allowed to use LINQ, you can use the Take() method. It returns a specified number of contiguous elements from the start of a sequence. We can use it with the String constructor, which takes an IEnumerable<T> to construct a new instance of the string. Note that you need to include the System.Linq namespace.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Linq; public static class StringExtensions { public static string Extract(this string input, int len) { return new string(input?.Take(len).ToArray()); } } public class Example { public static void Main() { string input = "Hello World"; string s = input.Extract(5); Console.WriteLine(s); // Hello } } |
That’s all about extracting the first few characters of a string in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)