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
How to get the last five characters of a string using Substring() in C#?
If your input string could be less than five characters long then you should be aware that string.Substring
will throw an ArgumentOutOfRangeException
if the startIndex
argument is negative.
To solve this potential problem you can use the following code:
string sub = input.Substring(Math.Max(0, input.Length - 5));
Or more explicitly:
public static string Right(string input, int length)
{
if (length >= input.Length)
{
return input;
}
else
{
return input.Substring(input.Length - length);
}
}
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
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
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.
Extract only right most n letters from a string
string SubString = MyString.Substring(MyString.Length-6);
How can I get the last 7 characters of a PHP string?
Use substr()
with a negative number for the 2nd argument.
$newstring = substr($dynamicstring, -7);
From the php docs:
string substr ( string $string , int $start [, int $length ] )
If start is negative, the returned string will start at the start'th character from the end of string.
Related Topics
Inheritance with Base Class Constructor with Parameters
What Is the Maximum Resolution of C# .Net Bitmap
Should I Store My Images in the Database or Folders
What Is the Use for Task.Fromresult<Tresult> in C#
What Is the "Cost" of .Net Reflection
Copy File on a Network Shared Drive
Creating Delegates Manually VS Using Action/Func Delegates
How to Capture the '#' Character on Different Locale Keyboards in Wpf/C#
Request Windows Vista Uac Elevation If Path Is Protected
Inconsistency in Divide-By-Zero Behavior Between Different Value Types
How to Get a Uri of the Image Stored in the Resources
Print Webbrowser Without Previewing I.E. Single Click Print
How Would You Make a Unique Filename by Adding a Number
Copy Rows from One Datatable to Another Datatable