Solved

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

Posted on 2011-09-21
4
187 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 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