Avatar of doramail05
doramail05Flag for Malaysia

asked on 

c# geting value from string from end despite the length

having a string that will increase its length over time,

therefore substring(thestring.Length - 6, 1);

wont actually work

is there a method in c# similar to vb's Strings.Right, or a regex?
ASP.NETC#

Avatar of undefined
Last Comment
Frank Helk
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

What do you mean, won't work?
If the string is 10 chars long then Length is 10, if 20 then Length is 20, if 12345 then length is 12345.
Avatar of doramail05
doramail05
Flag of Malaysia image

ASKER

"1119991234123"; = 13 in length    

(thestring - 6, 1)

= getting 2


if

"11199912341234"; = 14 in length

(thestring - 6, 1)

= getting 3
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Yes, so what is the problem?  The VB right function would behave the same way and you say you want something from the end of the string.
ASKER CERTIFIED SOLUTION
Avatar of Frank Helk
Frank Helk
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Do you want get 6th from right of string?

char[] getRight(string str,int postfromright)
{
   if (postfromright>=str.Length) return 0;
   else return str[str.Length-postfromright-1];
}
string getRight(string str,int postfromright,int count)
{
   if (postfromright>=str.Length+count) return "";
   else return str.SubString([str.Length-Post-1],count);
}

Open in new window

Avatar of doramail05
doramail05
Flag of Malaysia image

ASKER

guess this works fine for me

String s, t;
s = "111999123412301";
t = Strings.Right(s, 3);
char c = t.FirstOrDefault();
Response.Write(c);

Open in new window

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Odd.
In your 'self posted answer' you specify the final 3 chars of the string, in the question you were posting code that retrieved a string of length 1 part way in from the end and stating it didn't work because the string was growing in length.
If you always wanted the final 3 characters as a string then I would have told you how to do it in my very first post.
In future it will help you if you ask a question about what you want.

(If you don't understand a function - eg. substring - it is always useful to look it up in the help files, then you may not need to waste time even asking a question, it is clear there that the second parameter is the length of the string to return AND not supplying one will return everything until the end of the string)
Avatar of Frank Helk
Frank Helk
Flag of Germany image

@AndyAinscow:

Hmmmm- despite of the fact that his substring() example was not perfectly chosen (although he said that it won't work for him), he explicitly asked if there was a similar function to VB's Strings.Right() in C#. That was completely clear from my view, wasn't it ?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

@frankhelk
I'm not criticizing you.  It is just that the substring function will do do everything the Right function will do and more.  No need for any extra imports and quite possibly fewer lines of code as well depending on if there is a requirement for the 'and more'.  The reason that substring didn't work given was that the string was growing.  If substring wouldn't work in that case then Right should have even more problems.
Avatar of Frank Helk
Frank Helk
Flag of Germany image

@AndyAinscow:

I didn't take that as critic ... no need to apologize.

For clarification (no critc, too): I recommended that solution because it has some advantages:

Maybe faster: substring() has some more flexible options and therefore is more complex. That leads (possibly) to a bigger performance footprint ... which (i admit) might be neglectible depending on usage frequency. Besides of that, substring() needs some  extra math with "length-n" on the caller side, which is obsolete with Right() that does it internal.
Readability of code: If someone reads the code, the substring() variant would cause the reader to stumble a little bit, with the need for meditating about "what's done here" - If I had the need for Right() at many places, as the next best solution after importing the VB  version I would have encapsulated the substring() into a custom right() just to clarify my coding.

And the only extra code needed is the "using ..." directive at the beginning of the code. Not much of a sacrifice in my opinion, measured by at least the gain in readability. And when using Visual Studio, Strings.Right() is part of the package ... isn't it ?
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo