In VB there is a function called Right, which returns a string containing a specified number of characters from the right side of a string.
Is there a similar function in C# that does the same thing?
Thank you.
In VB there is a function called Right, which returns a string containing a specified number of characters from the right side of a string.
Is there a similar function in C# that does the same thing?
Thank you.
Update: As mentioned in the comments below my previous answer fails in case the string is shorter than the requested length (the Right()
in VB.net does not). So I've updated it a bit.
There is no similar method in C#, but you can add it with the following extension method which uses Substring()
instead:
static class Extensions
{
/// <summary>
/// Get substring of specified number of characters on the right.
/// </summary>
public static string Right(this string value, int length)
{
if (String.IsNullOrEmpty(value)) return string.Empty;
return value.Length <= length ? value : value.Substring(value.Length - length);
}
}
The method provided is copied from DotNetPearls and you can get additional infos there.
There is no built in function. You will have to do just a little work. Like this:
public static string Right(string original, int numberCharacters)
{
return original.Substring(original.Length - numberCharacters);
}
That will return just like Right
does in VB.
Hope this helps you! Code taken from: http://forums.asp.net/t/341166.aspx/1
you can use all the visual basic specific functions in C#
like this :-
Microsoft.VisualBasic.Strings.Right(s, 10);
you will have to reference the Microsoft.VisualBasic Assembly as well.
You can call this function from C# by importing the Microsoft.VisualBasic namespace.
But don't. Don't use .Right() from VB either. Using the newer .Substring()
method instead.