How to Get the Last Four Characters from a String in C#

Get last 3 characters of string

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

C# 8 way to get last n characters from a string

Others are correct in that it is just syntax sugar but there is some slight differences. They both call "Substring" however the hat version calls the String.Substring(Int32, Int32) signature and the later conventional version calls String.Substring(Int32) signature.

It's interesting that even though str[^2..] produces more code it seems to have slightly better performance in my quick benchmark.

var str = "foo";
var str2 = str[^2..];
========generates the following IL=============
nop
ldstr "foo"
stloc.0 // str
ldloc.0 // str
dup
callvirt String.get_Length ()
dup
ldc.i4.2
sub
stloc.2
ldloc.2
sub
stloc.3
ldloc.2
ldloc.3
callvirt String.Substring (Int32, Int32)
stloc.1 // str2
ldloc.1 // str2
call Console.WriteLine (String)
nop
ret

And the following conventional version.

var str = "foo";
var str2 = str.Substring(str.Length - 2);
========generates the following IL=============
nop
ldstr "foo"
stloc.0 // str
ldloc.0 // str
ldloc.0 // str
callvirt String.get_Length ()
ldc.i4.2
sub
callvirt String.Substring (Int32)
stloc.1 // str2
ldloc.1 // str2
call Console.WriteLine (String)
nop
ret

get all characters to right of last dash

You can get the position of the last - with str.LastIndexOf('-'). So the next step is obvious:

var result = str.Substring(str.LastIndexOf('-') + 1);

Correction:

As Brian states below, using this on a string with no dashes will result in the original string being returned.

Get the two first and last chars of a string C#

Starting C# 8.0 you can use array ranges:

public static class StringExtentions {
public static string FirstLastChars(this string str)
{
// If it's less that 4, return the entire string
if(str.Length < 4) return str;
return str[..2] + str[^2..];
}
}

Check solution here: https://dotnetfiddle.net/zBBT3U

Remove the last three characters from a string

read last 3 characters from string [Initially asked question]

You can use string.Substring and give it the starting index and it will get the substring starting from given index till end.

myString.Substring(myString.Length-3)

Retrieves a substring from this instance. The substring starts at a
specified character position. MSDN

Edit, for updated post

Remove last 3 characters from string [Updated question]

To remove the last three characters from the string you can use string.Substring(Int32, Int32) and give it the starting index 0 and end index three less than the string length. It will get the substring before last three characters.

myString = myString.Substring(0, myString.Length-3);

String.Substring Method (Int32, Int32)

Retrieves a substring from this instance. The substring starts at a
specified character position and has a specified length.

You can also using String.Remove(Int32) method to remove the last three characters by passing start index as length - 3, it will remove from this point to end of string.

myString = myString.Remove(myString.Length-3)

String.Remove Method (Int32)

Returns a new string in which all the characters in the current
instance, beginning at a specified position and continuing through the
last position, have been deleted

How to use EVAL to get the last four character of a string

You can use a below

<td><%# Eval("SSN").ToString().Length > 4 ?
Eval("SSN").ToString().Substring(Eval("SSN").ToString().Length - 4, 4)
:Eval("SSN").ToString() %></td>

Get Last Four word after splitting a string

public static class SplitExtension
{
public static string LastNItems(this string str, int nItem, char separator = '.')
{
return string.Join(separator.ToString(), str.Split(separator).Reverse().Take(nItem).Reverse());
}

public static string[] LastNItems(this string[] strArray, int nItem)
{
return strArray.Reverse().Take(nItem).Reverse().ToArray();
}
}

This will enable you to do

var str1 = "20180215.20150215.3.1.0.0";

var str1Last4 = str1.LastNItems(4); // "3.1.0.0"
var str1Last4Items = str1.Split('.').LastNItems(4); // ["3","1","0","0"]

or for completion

var str1Last4Items = str1.LastNItems(4).Split('.'); // ["3","1","0","0"]


Related Topics



Leave a reply



Submit