Solved

c# geting value from string from end despite the length

Posted on 2014-02-28
10
260 Views
Last Modified: 2014-03-03
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?
0
Comment
Question by:doramail05
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39894216
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.
0
 
LVL 1

Author Comment

by:doramail05
ID: 39894227
"1119991234123"; = 13 in length    

(thestring - 6, 1)

= getting 2


if

"11199912341234"; = 14 in length

(thestring - 6, 1)

= getting 3
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39894237
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.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 14

Accepted Solution

by:
frankhelk earned 500 total points
ID: 39894260
If you need Right(), you can simply use it right away (no pun intended :) in C# ....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;

namespace TestClass
{
    class Program
    {
        static void Main(string[] args)
        {
            String s,t;
            s = "This is a string !";
            t = Strings.Right(s, 3);
            Console.WriteLine(t);
        }
    }
}

Open in new window

That would write

g !

to the console.

Bsides of using Microsoft.VisualBasic you'll need to add a reference to Microsoft.VisualBasic to your project.

Have fun ...
0
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 39894289
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

0
 
LVL 1

Author Comment

by:doramail05
ID: 39899375
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

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39899663
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)
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 39899952
@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 ?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39899982
@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.
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 39900300
@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 ?
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET(C#) Eliminating weekends from a date range 2 45
Expression Evaluater 3 38
Change to file doesn't show up 16 55
JQuery Detecting page post back 6 12
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question