Wrox Programmer Forums
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics 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 November 21st, 2008, 04:05 PM
Authorized User
Join Date: Dec 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default More string problems

Ok, here goes. I'm have a string variable that's being returned to me as for money that's being returned as: USD1.30 (for 1 dollar thirty cents.) I need to strip off the first three characters (the USD part.) What's the easiest/best/cleanest way to do that? I tried Left and that didnt work. Also was thinking about doing a Right command on the string, but I'm not sure how many digits the money amount will be.

Any ideas??

Old November 21st, 2008, 04:21 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

Something like this would work:

Dim someVariable, someVariable2

someVariable = "USD1.30"
someVariable2 = Right(someVariable, (Len(someVariable) - 3))

The use of Right() in the assignment of someVariable2 says, starting from the Right most position of someVariable move X positions to the left where X is the length of someVariable less 3.

Response.Write will display 1.30 to the screen.


================================================== =========
Read this if you want to know how to get a correct reply for your question:
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Old November 21st, 2008, 05:19 PM
Friend of Wrox
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts

A bit easier than what DParsons suggested is simply:
    ugly = "USD1.30"
    pretty = Mid( ugly, 4 )
No reason to use RIGHT, which requires getting the length and then subtracting 3.
Just *start* at the character after the "USD" and presto.

*** HOWEVER ***

Is this REALLY a good idea????

What happens if at some point your "feed" changes and starts giving you
    USD 3.38
    3.38 USD
    3.38 US$

I think a much better way is to use a regular expression to strip out all of the characters that are not part of the actual numerical value. Thus:
Set reZap = New RegExp
reZap.Pattern = "[^\d\.\-]"
reZap.Global = True

ugly = "USD78.31"
pretty = CDbl( reZap.Replace( ugly, "" ) ) ' and SHOULD convert to a number when you do this!
You can use CCur( ) in place of CDbl( ), if you wish. But in any case, stop carrying the number around as a string. (EXCEPTION: If you will then *ONLY* Response.Write the value, you can leave it as a string. But if you do *ANY* calculation with it--or if you store it in a DB--be sure to convert it to a number.)
Old November 23rd, 2008, 07:24 PM
Friend of Wrox
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41

Once again assistance well above anybodys expectations.....

Wind is your friend

Similar Threads
Thread Thread Starter Forum Replies Last Post
problems with my SQL string dstein4d Access VBA 9 September 19th, 2008 01:50 PM
how to find a string in another string in vb6 satish_k VB How-To 3 March 30th, 2007 12:17 PM
Problems with string functions and Access 2000 giggs Access VBA 5 February 25th, 2004 09:43 AM
ASP.NET MSDE Connection string problems Bagheer Classic ASP Databases 0 August 27th, 2003 08:02 PM

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