Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 2nd, 2007, 03:48 PM
Authorized User
 
Join Date: Jan 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default comparing strings

Ok I am writing a sort of point of sales program. I need to know how i can compare what the user entered as a zip code...and compare the first three characters to determine shippping charges.

Example:

Shipping Charges:
Zip Code:
    700xx, 701xx, 702xx, 703xx, 704xx 12.00
    705xx 8.00
    706xx 5.00
    707xx, 708xx, 709xx 10.00
    Any others in state of LA 12.00
    Any others outside of LA 25.00

Any ideas? I would like to check the first three letters to verify that they are in state and determine the third using maybe a switch statement to apply the shipping charges but i dont know how.
 
Old February 2nd, 2007, 03:53 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

string sInput = "";
string sZip = "";

sInput = txt.Text;
sZip = sInput.Substring(0, 3)

sZip now contains the first 3 characters of the users input. Of course you can skip this step and do something like

if(sInput.Substring(0, 3) == "xxx")
{
//do something
}

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
 
Old February 2nd, 2007, 05:01 PM
Authorized User
 
Join Date: Jan 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

sInput = txt.Text;
sZip = sInput.Substring(0, 3)

Ok i think mine will be a little bit different, but i think i understand it. I am going to parse the value from input entered in a textbox, and assign it to my input string. The second line is me moving the first three chars into my zip string. Correct? Also I am required to use a switch statement in this prog and i think this is the best place to do it. Can i do something like:

switch(szip)
{
     case '703':
         {
          //do something
          break;
         }
      case '704':
         {
          //do something else
          break;
         }

will that work???

Seymour
 
Old February 2nd, 2007, 05:05 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Yes. Substring, in this instance, takes 2 parameters: a start index and a number of places to move from that index.

Also, make sure you check the length of the string being passed in if, for example, a user entered 77 as the zip, you will throw an exception because there is no 3rd position in the string.


================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
 
Old February 6th, 2007, 04:43 PM
Authorized User
 
Join Date: Jan 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

OK one more thing. How do I check an input for a specific number of characters?
I understand the concept, it's the syntax i am unsure of.
 
Old February 6th, 2007, 04:49 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

It would be something like:
if(sZip.Length < 3)
{
//do something
}
else
{
//switch
}

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing strings and integers pcase XSLT 2 December 1st, 2007 06:55 AM
Comparing two strings shasto100 XSLT 2 April 28th, 2006 08:10 AM
Comparing Arrays mattastic Javascript 2 January 27th, 2006 05:48 AM
comparing strings treasacrowe Classic ASP Databases 5 September 11th, 2004 09:48 AM
Comparing DataSets. jitu ADO.NET 1 June 7th, 2004 11:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.