Solved

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

Posted on 2011-09-21
4
184 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
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
String e = reader.ReadElementContentAsString();
String formattedString = e.Substring(e.Length - 9, 3);
String formattedString1 = e.Substring(e.Length - 6, 6);
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now