Solved

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

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

617 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