Reputation: 803
How can I get only the last 3 character out from a given string?
Example input: AM0122200204
Expected result: 204
Upvotes: 71
Views: 232744
Reputation: 8877
Many years lates..but here is my go at it. There are some here that accept smaller sizes and others that do not. The examples below show the output for all. I also added some benchmarks.net timings for the longer example.
string longgText = "this is some text in a string";
string shortText = "hi";
string result;
/* Function Result Time/Source*/
/***************************************************************************************/
result = longgText.Substring(Math.Max(0,longgText.Length-6));//"string" 9.6 ns
result = shortText.Substring(Math.Max(0,shortText.Length-6));//"hi Sunsetquest
result = longgText[^(Math.Min(6, longgText.Length))..]; // "string" 9.9 ns
result = shortText[^(Math.Min(6, shortText.Length))..]; // "hi" Sunsetquest
result = longgText.PadLeft(6)[^6..]; // "string" 12.7 ns
result = shortText.PadLeft(6)[^6..]; // " hi"
result = longgText[^6..]; // "string" 9.0 ns
result = shortText[^6..]; // [Exception] Visual Sharp
result=longgText.AsSpan().Slice(longgText.Length-6).ToString();//"string" 13.0 ns
result=shortText.AsSpan().Slice(shortText.Length-6).ToString();//[Exception] Lemon Sky
result = longgText.Substring(longgText.Length - 6); // "string" 8.8 ns
result = shortText.Substring(shortText.Length - 6); // [Exception] Ian
result = Regex.Match(longgText, @"(.{6})\s*$").ToString(); // "string" 3600 ns
result = Regex.Match(shortText, @"(.{6})\s*$").ToString(); // "" Hari Prasad
Upvotes: 2
Reputation: 3996
From C# 8 Indices and ranges
Last 3 digits of "AM0122200204" string:
"AM0122200204"[^3..]
Upvotes: 26
Reputation: 697
With the introduction of Span
s in C# 7.3 and .NET Core 2.1 we now have an additional way of implementing this task without additional memory allocations. The code would look as follows:
var input = "AM0122200204";
var result = input
.AsSpan()
.Slice(input.Length - 3);
In traditional code, every string manipulation creates a new string on the heap. When doing heavy string-based manipulations like in compilers or parsers, this can quickly become a bottleneck.
In the code above, .AsSpan()
creates a safe, array-like structure pointing to the desired memory region inside the original string. The resulting ReadOnlySpan
is accepted by many method overloads in libraries.
For example, we can parse the last 3 digits using int.Parse
:
int value = int.Parse(result)
Upvotes: 1
Reputation: 16986
Many ways this can be achieved.
Simple approach should be taking Substring
of an input string.
var result = input.Substring(input.Length - 3);
Another approach using Regular Expression
to extract last 3 characters.
var result = Regex.Match(input,@"(.{3})\s*$");
Working Demo
Upvotes: 169
Reputation: 30823
The easiest way would be using Substring
string str = "AM0122200204";
string substr = str.Substring(str.Length - 3);
Using the overload with one int
as I put would get the substring
of a string
, starting from the index int
. In your case being str.Length - 3
, since you want to get the last three chars.
Upvotes: 27