21

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.

brightintro
  • 1,016
  • 1
  • 11
  • 16
Neo
  • 481
  • 2
  • 9
  • 24

4 Answers4

45

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.

Chief Wiggum
  • 2,784
  • 2
  • 31
  • 44
  • 1
    VB.NET Right function won't fail if length parameter is greater than the string's length. – muratgu May 28 '13 at 02:35
  • That can well be. The question is what result we wan't if we try to get more characters from the string that it actually holds. I personally prefer an exception. But you are right, for a 'perfect' copy of the method we would then have to validate the length first and check for null as well. – Chief Wiggum May 28 '13 at 02:40
  • See Also: Left Function https://stackoverflow.com/questions/7574606/left-function-in-c-sharp – viggity Sep 22 '21 at 12:38
11

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

StuartQ
  • 3,739
  • 1
  • 26
  • 23
FrostyFire
  • 3,212
  • 3
  • 29
  • 53
9

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.

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • 2
    this not a good way to do it. You should not rely on importing the VB assembly unless their is no other way. – FrostyFire May 28 '13 at 02:10
  • also, the VB function relies on the c# Substring function, so really, might as well just extend the substring function. see http://referencesource.microsoft.com/Microsoft.VisualBasic/Strings.vb.html#0dbb15fffce19341 and when you click on `Substring` strings.cs is opened up! – Our Man in Bananas Dec 11 '14 at 15:14
  • 3
    its not C#s, its .NETs . The VB lib is an extension lib, and if you are porting VB code that makes a lot of use of the VB extension lib then this is a good way to go, or you can slowly rewrite it into vanilla .NET – Keith Nicholas Dec 11 '14 at 18:56
  • For what it's worth , it's an interesting answer :) – donkz Feb 24 '15 at 22:49
5

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794