VBA. Array for search or replace - arrays

Need to found any symbol of array.
For example:
replace(string,[a,b,c,e,f,g],"a1b2c3d4e567");
result = "1234567"
How do it ?

If your goal is to remove all non-numeric characters, the following will work:
' Added reference for Microsoft VBScript Regular Expressions 5.5
Const s As String = "a1b2c3d4e567"
Dim regex2 As New RegExp
Dim a As String
regex2.Global = True
regex2.Pattern = "[^0-9]"
Dim a As String = regex2.Replace(s, "")
MsgBox (a) ' Outputs 1234567
If you are looking for specific characters, change the pattern.

AFAIK you are going to have to do this by consecutive calls to replace
result = "a1b2c3d4e567"
result = replace(result,"a","")
result = replace(result,"b","")
result = replace(result,"c","")
etc

Related

How to filter String Array in VB.Net?

What is VB.Net code to filter a String Array ?
I use following code
Imports System.Reflection
Dim ass As Assembly = Assembly.GetExecutingAssembly()
Dim resourceName() As String = ass.GetManifestResourceNames()
that return a String array
How can I filter resourceName() variable ?
I tried following lines of code
Dim sNameList() As String
= resourceName.FindAll(Function(x As String) x.EndsWith("JavaScript.js"))
but compiler return following error
BC36625: Lambda expression cannot be converted to 'T()' because 'T()' is not a delegate type
How can I correct this error ?
Is there another solution to solve my problem ?
Dim sNameList = resourceName.Where(Function(s) s.EndsWith("JavaScript.js"))
In that case, sNameList is an IEnumerable(Of String), which is all you need if you intend to use a For Each loop over it. If you genuinely need an array:
Dim sNameList = resourceName.Where(Function(s) s.EndsWith("JavaScript.js")).ToArray()
The reason that your existing code didn't work is that Array.FindAll is Shared and so you call it on the Array class, not an array instance:
Dim sNameList = Array.FindAll(resourceName, Function(s) s.EndsWith("JavaScript.js"))

Add Single quotes where string contains comma in AngularJS

I have a string which contains values like
var string = A,B,C
Here I want to add single quote for each comma value, expected result should be as below
output = 'A','B','C'
My angular code is
var data = {
output : this.string.split("\',"),
}
which is giving result as
["A,B,C"]
Could anyone please help on this how can I get desired output.
I am understanding your code as
var string = "A,B,C"; // because string should be in this format.
and you just need to replace "\'," from your split function to "," which will give you an array like this
var out = string.split(",");
console.log(out);
[ 'A', 'B', 'C' ] // this is the output.
as split function searches for the given expression and split the string into array.
but if you just want to modify the string without making it in array then you can use the below trick
var out = "'" + string.replace(/,/g, "','") + "'";
console.log(out);
'A','B','C' // result as u mentioned and this is of string type.

How to retrieve a VB Control instance, given its Name and Index?

If I have a string "cboEnoughMoney(0)", is it possible to retrieve the control from a control array that I have in my form, cboEnoughMoney?
In my code, I try to refer to the control in this way:
frm.Controls(sControlName).ListIndex = -1
sControlName in this case is "cboEnoughMoney(0)" and I get an error that the control was not found.
EDIT: One of the issue's i'm having is trying to as above set the .listindex to -1, another one is where I actually try to get and set value to the combobox. In a function - i pass form name as parameter called frm.
In the code what has been done is this....
dim ctlData as string
If Frm.Controls(RS!CTRLname).ListIndex > 0 Then
CtlData = Frm.Controls(RS!CTRLname).Value
Else
CtlData = ""
End If
Any idea how I'd be able to do that with cboEnoughMoney(0)....
I assume I could follow your example and check
if instr(1,RS!CTRLname, "(")
but if there is, how would I refer to that particular control in frm
It is just enough to look in the VB Properties Window: if you search "cboEnoughMoney(0)" you will find "cboEnoughMoney"(0) (without double quotes). Still the name is the same as a non-array control.
So, it is perfectly legal to refer to a VB Control with Name and Index, no need for a controls loop here:
If isControlsArray Then
Set ctrl = Me.Controls(Name)(Index) '// array
Else
Set ctrl = Me.Controls(Name) '// non array
End If
You cannot use the (index) part in a lookup via .Controls.
You can loop them manually & given that you can safely assume any control name with a ( is an array member and thus will have an Index property match on that:
Private Function GetControl(ByVal name As String) As VB.Control
Dim pos As Long: pos = InStr(name, "(")
If pos = 0 Then
Set GetControl = Me.Controls(name) '// non array
Else
Dim index As Long
index = Val(Mid$(name, pos + 1)) '// get index #
name = Left$(name, pos - 1) '// get base name
For Each GetControl In Me.Controls
If (GetControl.name = name) Then
If (GetControl.index = index) Then Exit Function
End If
Next
Set GetControl = Nothing
End If
End Function
And then:
GetControl("cboEnoughMoney(1)").ListIndex = -1

Use the string stored in a Variant in an Array

I'm trying to use the values inside of a Variant.
An example of the code:
Dim Holder as Variant
Holder = "1,1,1,1,1"
Later I will be using this Variant inside of an Array().
The Declaration is like this:
.TextFileColumnDataTypes = Array(Holder)
There was an error right after this
Run-Time Error '5':
Invalid Procedure call or argument*
Is there a way to insert the Variant's values into Array(<Here?).
Holder's value is not constant, it will change depending on a function I created.
A way to accomplish this is as suggested in comments, using the split
If you are getting an error I am guessing that you are not using it correctly.
This is how the code should look.
dim i as long
dim var
Holder = "1,1,1,1,1"
var = Split(Holder,",")
for i = 0 to 4
.TextFileColumnDataTypes = var(i)
next i

Vbscript passing a variable from access database to email

Why do I have to URLEncode a string with spaces from a database to pass to email (MAILTO subject) when if you create the mailto string manually you don't have to.
Eg: example
works fine but if I get the subject from a database it only passes the first word "Hello" and trims the rest unless I use Server.URLEncode(stringfromdatabase). this inserts a + inplace of the spaces.
Is there a method that allows me to pass a string with spaces from a database to an email client that doesn't need to modify the string (adding + or %20)?
Utilize a replace function to swap the spaces with %20. In your example the variable would be
replace(stringfromdatabase," ", "%20")
This worked for me using VBScript in the ASP page I was struggling with.
all sorted thank you used the following
Function CleanString(strData)
'Replace invalid strings.
strData = Replace(strData, "'", "")
strData = Replace(strData, "#", "")
strData = Replace(strData, "&", "and")
CleanString = Trim(strData)
End Function
Function strClean (strtoclean)
Dim objRegExp, outputStr
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "((?![a-zA-Z0-9\s*,]).)+"
outputStr = objRegExp.Replace(strtoclean, "-")
objRegExp.Pattern = "\-+"
outputStr = objRegExp.Replace(outputStr, "")
strClean = outputStr
End Function

Resources