Solved

how to parse a string in C#?

Posted on 2011-03-10
10
446 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
[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
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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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