Remove last N characters from string 
Author Message
 Remove last N characters from string

Hey all,

I'm having trouble figuring out how to remove the last N characters
from a string.  I've tried the Left() function but that returns the
first N characters and that's not what I want.  I'm getting a date
from the DB in this format 1/1/2002.  I want to strip off the /2002
just for the display purposes.  The problem is if I only display the
first 3 chars., what happens when the day has 2 digits instead of 1
(ex. 1/12/2002 instead of 1/1/2002)??  I thought of the InStr function
to remove it but I don't want have to remember to change that code
every year.
What is the best way to go about this?? I appriciate everyone's help!

-JD
http://www.*-*-*.com/



Wed, 02 Feb 2005 23:56:18 GMT  
 Remove last N characters from string

Quote:

> I'm having trouble figuring out how to remove the last N characters
> from a string.  I've tried the Left() function but that returns the
> first N characters and that's not what I want.  I'm getting a date
> from the DB in this format 1/1/2002.  I want to strip off the /2002
> just for the display purposes.  The problem is if I only display the
> first 3 chars., what happens when the day has 2 digits instead of 1
> (ex. 1/12/2002 instead of 1/1/2002)??  I thought of the InStr function
> to remove it but I don't want have to remember to change that code
> every year.
> What is the best way to go about this?? I appriciate everyone's help!

Hi

Combine Left with Len:

sDate = "1/1/2002"

sDate = Left(sDate, Len(sDate) -5)

WScript.Echo sDate

--
torgeir



Thu, 03 Feb 2005 00:10:41 GMT  
 Remove last N characters from string
Can't you just use the Day() and Month() functions in VBScript:

<%
    TargetDate = CDate("1/1/2002")
    Response.Write(Month(TargetDate) & "/" & Day(TargetDate))
%>


Quote:
> Hey all,

> I'm having trouble figuring out how to remove the last N characters
> from a string.  I've tried the Left() function but that returns the
> first N characters and that's not what I want.  I'm getting a date
> from the DB in this format 1/1/2002.  I want to strip off the /2002
> just for the display purposes.  The problem is if I only display the
> first 3 chars., what happens when the day has 2 digits instead of 1
> (ex. 1/12/2002 instead of 1/1/2002)??  I thought of the InStr function
> to remove it but I don't want have to remember to change that code
> every year.
> What is the best way to go about this?? I appriciate everyone's help!

> -JD
> http://www.jasondeanonline.com



Thu, 03 Feb 2005 00:27:30 GMT  
 Remove last N characters from string
Function RStrip(s, n)
  RStrip = Left(s, Len(s)-n)
End Function

For Each test In Array("1/12/2002", "01/12/2002", "12/1/2002")
  WScript.echo RStrip(test, 5)
Next

--
There is a pleasure sure, in being mad, which none but madmen know. -John
Dryden

=-=-=
Steve
-=-=-


Quote:
> Hey all,

> I'm having trouble figuring out how to remove the last N characters
> from a string.  I've tried the Left() function but that returns the
> first N characters and that's not what I want.  I'm getting a date
> from the DB in this format 1/1/2002.  I want to strip off the /2002
> just for the display purposes.  The problem is if I only display the
> first 3 chars., what happens when the day has 2 digits instead of 1
> (ex. 1/12/2002 instead of 1/1/2002)??  I thought of the InStr function
> to remove it but I don't want have to remember to change that code
> every year.
> What is the best way to go about this?? I appriciate everyone's help!

> -JD
> http://www.jasondeanonline.com



Thu, 03 Feb 2005 00:16:13 GMT  
 Remove last N characters from string
Yes, this is much more reliable, especially when dealing with regional
settings and locale issues (where year, for example, might be two digits,
and might come first)...


Quote:
> Can't you just use the Day() and Month() functions in VBScript:

> <%
>     TargetDate = CDate("1/1/2002")
>     Response.Write(Month(TargetDate) & "/" & Day(TargetDate))
> %>



Thu, 03 Feb 2005 02:04:48 GMT  
 Remove last N characters from string
The Day(), Month() functions are the best way to go.

If one wanted a more generic way of doing this (if it wasn't a date for
example) then you could split this into an array, splitting at the "/"
character and then use the relevant bits.

So, for example

astrDate() = split("1/1/2002", "/")
' gives astrDate(0) = "1", astrDate(1) = "1", astrDate(2) = "2002"

Griff


Quote:
> Can't you just use the Day() and Month() functions in VBScript:

> <%
>     TargetDate = CDate("1/1/2002")
>     Response.Write(Month(TargetDate) & "/" & Day(TargetDate))
> %>



Fri, 04 Feb 2005 17:22:32 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Remove last N characters from string

2. Cannot find stored procedure

3. Mouse and Graphics

4. Remove last 4 characters from a string

5. memory scan

6. Removing last word of string

7. Extracting the last N characters of a string

8. DBGrid with a SQL-query on "large" numbers of records

9. Answers please :)

10. Removing characters from strings

11. Removing characters from strings

12. Last Character in a string?

 

 
Powered by phpBB® Forum Software