Link to home
Start Free TrialLog in
Avatar of aotyehel
aotyehel

asked on

Remove numeric characters from a string

I have this code which should remove the numeric characters - but I keep getting the error:

Microsoft VBScript compilation error '800a0401'

Expected end of statement

/piac/test-remove.asp, line 8

Dim initialString As String = "XXXXXX-06-X-1234"
------------------^

I also saw another solution which I tried:

<%
Reg = New RegExp
Reg.Global=true
Reg.IgnoreCase=true
Reg.Pattern="([0-9]$[-\s]*[0-9]$)" 'this is the important part - it finds the string of characters

Function RemoveNumbers(objString)
     RemoveNumbers=Reg.Replace(objString, "")
End Function
%>

<p>Address with no numbers: <%=RemoveNumbers("Sport utca 3")%></p>

but that returned the error:

Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method


All I want to do is take a value for a field from a DB that is a text string, remove the numbers and then render the field without the numbers.

Any help would be apreciated.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<%
Dim initialString As String = "XXXXXX-06-X-1234"
Dim nonNumericCharacters As New System.Text.RegularExpressions.Regex(@"\D")
Dim numericOnlyString As String = nonNumericCharacters.Replace(initialString, String.Empty)
%>
</head>
 
<body>
<%=numericOnlyString%>
</body>
</html>

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image


Dim initialString As String = "XXXXXX-06-X-1234"
is vb.net.
in vbs, you need to put 2 lines:


Dim initialString
initialString = "XXXXXX-06-X-1234"
Avatar of aotyehel
aotyehel

ASKER

That partially helped...

but how do I deal with the other lines?

The attached code is what I have.

The following error is returned:

Microsoft VBScript compilation error '800a0408'

Invalid character

/piac/test-remove.asp, line 11

nonNumericCharacters = New System.Text.RegularExpressions.Regex(@"\D")
----------------------------------------------------------------^



<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<%
Dim initialString 
initialString = "XXXXXX-06-X-1234"
Dim nonNumericCharacters 
nonNumericCharacters = New System.Text.RegularExpressions.Regex(@"\D")
Dim numericOnlyString 
numericOnlyString = nonNumericCharacters.Replace(initialString, String.Empty)
%>
</head>
 
<body>
<%=numericOnlyString%>
</body>
</html>

Open in new window

remove the @. that is for .net to make sure the \ inside the string would be use as such, and not as escape character. which is not not needed in vbscript
Error

Microsoft VBScript runtime error '800a01fa'

Class not defined: 'System'


<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<%
Dim initialString 
initialString = "XXXXXX-06-X-1234"
Dim nonNumericCharacters 
nonNumericCharacters = New System.Text.RegularExpressions.Regex("\D")
Dim numericOnlyString 
numericOnlyString = nonNumericCharacters.Replace(initialString, String.Empty)
%>
</head>
 
<body>
<%=numericOnlyString%>
</body>
</html>

Open in new window

>nonNumericCharacters = New System.Text.RegularExpressions.Regex("\D")
that is part of the .net framework, which I fear you cannot use directly from vbs like that.


but, there is some alternative for vbs:
http://msdn.microsoft.com/en-us/library/ms974570.aspx

OK. Then I need to change my question.

Can someone write me some code for classic ASP with VBScript that can take a string and remove (or replace with spaces) any numbers then return the altered string.

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Final code:

<%
Dim initialString
initialString = "XXXXXX-06-X-1234"

Dim i

for i = 0 to 9
  initialString = replace(initialString , "" & i, " ")
next

%>

No 'As String' in VBScript