Solved

how to parse a string in C#?

Posted on 2011-03-10
10
443 Views
Last Modified: 2013-12-17

Hello,

I need to create an entry box for customer to enter door sizes. I'm not sure if there is a better way but I am thinking of having a text box allowing customer enter something like:  12 1/2 x 74 7/8  (inch)  which is width x length


Few issues:

1) How to parse this since I need to use this in a formula. I need to extract 12, then 1/2 then 74 and finally 7/8.
2) How can I avoid getting garbage from this text box on page?


Thanks,
ak

0
Comment
Question by:akohan
10 Comments
 
LVL 6

Expert Comment

by:Webmonkey
ID: 35100156
1.  Check the value to ensure it only contains numbers, 1 "x", and backslashes.
2.  Split the string into a string array delimited by the "x".
3.  Split each of the two elements into two additional string arrays delimited by the " " (space).
4.  Convert each of the remaining strings to integers (So "12" becomes 12, "1/2" becomes 0.5, etc.)
0
 
LVL 7

Expert Comment

by:jdavistx
ID: 35100225
There may be something ASP-specific, but outside of that...

1) You could only allow numerical digits to be entered to your textboxes.  In the KeyPress event, you could cancel the event if the value isn't a number.

2) If the door sizes you manufacturer are bounded, you could instead use ComboBoxes with pre-populated values representing the possible ranges of values for each particular value you need.

3) You could validate the input after it's been entered with a regular expression.  If it doesn't meet some criteria, clear it and start over.

To parse your data, you can use the various .TryParse() methods from int.TryParse and double.TryParse()
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35100228
I would suggest you use multiple text boxes, one for width and one for height, and require the user to enter fractional inches as decimal values, not fractions.
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 35100726
Completely agree with tgerbert.

And probably I'd use numeric up-down control instead of a text box, this may prevent from to much checks and complex parsing.

something like:

http://www.asp.net/ajax/ajaxcontroltoolkit/samples/NumericUpDown/NumericUpDown.aspx
0
 

Author Comment

by:akohan
ID: 35110407

I thought about using something like what Tgerbert and Anarki addressed here but door sizes could be customized. so it should be something that customer might need to enter base on his needs.  Like text on t-shirts that could be anything.

Thanks.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35110494
It can't really be "anything," it's always a height and width, which are always numbers.
Untitled.png
0
 

Author Comment

by:akohan
ID: 35110533



>> require the user to enter fractional inches as decimal values, not fractions.

In this case I need to accept fractions as well.

Regards.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35110583
Same thing still applies.

Though I think I'd recommend you stick to decimals.
Untitled.png
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 35110707
You could use something like below to parse the fractional piece into a decimal value.  I assume you're going to ultimately want it as a decimal so you can perform calculations, sort, etc, plus this provides a way to verify something sane was entered and not "monkey/zebra"  The Regular Expression I used just looks for a string with between 1 and 3 digits, followed by a \ or a / followed by 1 to 3 decimal digits (thus 1/1000 is considered invalid).

static float FractionToSingle(string fraction)
{
	Regex regex = new Regex(@"^\s*(\d{1,3})\s*[/\\]\s*(\d{1,3})\s*$");

	Match m = regex.Match(fraction);

	if (!m.Success)
		throw new ArgumentException("Fractional value is not in a supported format.", "fraction");

	float numerator = Single.Parse(m.Groups[1].Value);
	float denominator = Single.Parse(m.Groups[2].Value);

	if (denominator == 0)
		throw new DivideByZeroException();

	return numerator / denominator;
}

Open in new window

0
 

Author Closing Comment

by:akohan
ID: 35238327
Thanks
0

Featured Post

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
VB.Net creating Contact in Outlook 1 47
ASP.NET MVC -Update model when click on <li> element 29 65
need to convert function to c# 5 40
C# XML Get Values 4 33
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 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