?
Solved

how to parse a string in C#?

Posted on 2011-03-10
10
Medium Priority
?
452 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
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 
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 2000 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

Technology Partners: 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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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