Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to assign values to 2 variables based on the displacement from the right side using C#?

Posted on 2011-09-21
4
Medium Priority
?
191 Views
Last Modified: 2013-12-17
I am developing a C# Console application using Visual Studio 2005.
My code is as follows:

String e = reader.ReadElementContentAsString();
String formattedString = e.Substring(6, 3);
formattedString1 = e.Substring(9, 6);

How could I change the code and replace these statements to assign
the rightmost 6 positions to formattedformattedString1 and
the 3 positions adjacent to the left of formattedString1 to variable formattedString?  

For example:

string E = 123456789
formattedString = 123
formattedString1 = 456789
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40
ID: 36577369
String e = reader.ReadElementContentAsString();
String formattedString = e.Substring(0, 3);
formattedString1 = e.Substring(E.Length - 6, 6);
0
 
LVL 2

Accepted Solution

by:
EndersDev earned 2000 total points
ID: 36577569
String e = reader.ReadElementContentAsString();
String formattedString = e.Substring(e.Length - 9, 3);
String formattedString1 = e.Substring(e.Length - 6, 6);
0
 
LVL 40
ID: 36577588
EndersDev solution is the right one.

I only looked at the expected result and did not read the question carefully. Mine would work in that situation, but not in other situations, unless the string is always of a fixed length.
0
 
LVL 13

Expert Comment

by:jonnidip
ID: 36578387
You can consider using this extension method:
        public static String Right(this String s, Int32 length)
        {
            length = Math.Max(length, 0);

            if (s.Length > length)
                return s.Substring(s.Length - length, length);
            else
                return s;
        }

Open in new window


Regards.
0

Featured Post

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

730 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