Solved

how to parse a string in C#?

Posted on 2011-03-10
10
438 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
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
Comment Utility

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 33

Expert Comment

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

Author Comment

by:akohan
Comment Utility



>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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

14 Experts available now in Live!

Get 1:1 Help Now