Search code examples
replacevbscriptasp-classic

Classic ASP - Consolidating A Group of Years


I currently use a template generator built in Classic ASP. It takes values in from a basic form and simply re-populates the template with those values, so the code can easily be copied and pasted on eBay, Amazon, etc. It also will generate the title for the listing.

The particular category of interest today is car wheels. Each wheel fits a certain span of years of the vehicle. Some wheels fit such a wide range of years that the title becomes stuffed with just years and doesn't leave any room for the rest. Here's an example:

Dodge Ram 1500 2002 2003 2004 2005 2006 2007 2008 2009 Used OEM Wheel

So to get around this, I wrote some code to shave off the beginning "20" of the year for each of the years between the first and last. So it would look like this:

Dodge Ram 1500 2002 03 04 05 06 07 08 2009 Used OEM Wheel

This shaves off enough extra characters so I can fit more useful information in the title before eBay cuts it off. However, now the problem. In the code, I am using a simple replace to shave off the first two digits of any 19XX years or 20XX years. In doing so, it also removes the year 2019 and 2020. Obviously the replace command is just doing its job, and I KNOW there is a better way with RegEx, however I am unfamiliar with the syntax completely. Here is the code I have:

if len(r("item_y1")) > 4 then
    startyear0 = split(r("item_y1"),"-")
    startyear = int(startyear0(0))
    stopyear = int(startyear0(1))

    howmanyyears = stopyear - startyear
    
    for i = 1 to howmanyyears
        allyears = allyears & " " & (startyear + i)
    next 
    
    yearspan = stopyear-startyear
    
    if yearspan > 4 then
        allyears = replace(allyears,"19","")
        allyears = replace(allyears,"20","")
        allyears = Mid(allyears, 1, len(allyears) - 2)
        fullyears = startyear & allyears & stopyear
    else
        fullyears = startyear & allyears
    end if
end if

The "item_y1" value is the year span, collected as: 2005-2010

Any help to get me on the right path would be MUCH appreciated! Thank you!


Solution

  • You could try a function like this to format the years value instead of using Replace().

    Function FormatYears(years)
        Dim result: result = years
        Dim data: data = Split(years, "-")
        If IsArray(data) Then
            result = Right(data(0), 2) & "-" & Right(data(1), 2)
        End If
        FormatYears = result
    End Function
    
    WScript.Echo FormatYears("1999-2000")
    

    Output:

    99-00