Solved

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

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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