Create an extension method. Put the following in a C# code file in App_Code and call it StringExtensions.cs:
public static class StringExtensions
{
public static string Right(this string s, int numberCharacters){
return s.Substring(s.Length - numberCharacters);
}
}
Then call it like this:
var input = "a string to test";
@input.Right(4) // will result in "test"
wavemaster
Participant
1291 Points
1128 Posts
how to get the last X characters from a string
Nov 05, 2012 02:44 AM|LINK
I found something on dotnetperl that said:
string.Right(4)
That doesn't work.
But what is the syntax?
heyitsme
Participant
1135 Points
410 Posts
Re: how to get the last X characters from a string
Nov 05, 2012 02:59 AM|LINK
dim str as String="yourtext"
to get last 3 characters use
str=str.Substring(str.Length-3,3)
Regards
Sangamesh Belawatgi,
Software Developer,
Bangalore - INDIA
dinord2005
Member
160 Points
34 Posts
Re: how to get the last X characters from a string
Nov 05, 2012 07:46 AM|LINK
Hello!!
In this link you have the good syntax in C# : http://www.csharphelp.com/2007/07/c-left-right-and-mid-functions/
My best Regards
RameshRajend...
Star
7983 Points
2099 Posts
Re: how to get the last X characters from a string
Nov 05, 2012 07:50 AM|LINK
Hai
try this
string = "James"
string = "peppers"
string c = right (string, 1);
or
look this
http://www.w3schools.com/jsref/jsref_substring.asp
Thank u
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: how to get the last X characters from a string
Nov 05, 2012 08:25 AM|LINK
Create an extension method. Put the following in a C# code file in App_Code and call it StringExtensions.cs:
public static class StringExtensions { public static string Right(this string s, int numberCharacters){ return s.Substring(s.Length - numberCharacters); } }Then call it like this:
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
wavemaster
Participant
1291 Points
1128 Posts
Re: how to get the last X characters from a string
Nov 06, 2012 02:08 AM|LINK
This works!