Solved

how to parse a string in C#?

Posted on 2011-03-10
10
447 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
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!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
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…

724 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