I have the following format:
Value1 is {0} and Value2 is {1}.
I need to replace the numbers in the brackets with strings. This is easily done in most languages using string.Format or something along those lines. How can I do this using only vbscript?
I've tried:
Replace (strFormat, "{0}", value1)
Replace (strFormat, "{1}", value2)
It does not work. Any solutions?
Replace (strFormat, "{0}", value1)
Based on your code snip, I'm guessing you believe Replace mutates strFormat directly. It doesn't work like that; You assign the result to the original variable like this:
strFormat = Replace (strFormat, "{0}", value1)
You can also assign to another variable to store the changed results, like this:
strFormat2 = Replace (strFormat, "{0}", value1)
I wanted something similar and didn't like any of these answers as they meant multiple lines for each value (Ignoring Beaner's answer is for the wrong language!) so I created the following:
Public Function StrFormat(FormatString, Arguments())
Dim Value, CurArgNum
StrFormat = FormatString
CurArgNum = 0
For Each Value In Arguments
StrFormat = Replace(StrFormat, "{" & CurArgNum & "}", Value)
CurArgNum = CurArgNum + 1
Next
End Function
You can use the following then (note that you need to add "Array()" around your variables):
formatString = "Test '{0}', '{2}', '{1}' and {0} again!"
Response.Write StrFormat(formatString, Array(1, 2, "three", "Unused"))
Response.Write StrFormat(formatString, Array(4, 5, "six", "Unused"))
Which will output what you expect:
Test '1', 'three', '2' and 1 again!
Test '4', 'six', '5' and 4 again!
Hope this feels a bit more natural for people from other languages.
As none of the answers so far addresses the problem of formatting (as opposed
to interpolating/splicing strings into strings):
This simple Class:
Class cFormat
Private m_oSB
Private Sub Class_Initialize()
Set m_oSB = CreateObject("System.Text.StringBuilder")
End Sub ' Class_Initialize
Public Function formatOne(sFmt, vElm)
m_oSB.AppendFormat sFmt, vElm
formatOne = m_oSB.ToString()
m_oSB.Length = 0
End Function ' formatOne
Public Function formatArray(sFmt, aElms)
m_oSB.AppendFormat_4 sFmt, (aElms)
formatArray = m_oSB.ToString()
m_oSB.Length = 0
End Function ' formatArray
End Class ' cFormat
harness .NET formatting for VBScript via COM. Now you can do:
-------- Interpolation
Use |Value1 is {0} and Value2 is {1}.|
to get |Value1 is zero and Value2 is one.|
from |zero one|
Use |{0} x 2 => {0}{0}|
to get |once x 2 => onceonce|
from |once|
-------- Cherrypicking
Use |{6,4}: [{0}, {2}, {4}]|
to get |even: [0, 2, 4]|
from |0 1 2 3 4 5 even odd|
Use |{7,4}: [{5}, {3}, {1}]|
to get | odd: [5, 3, 1]|
from |0 1 2 3 4 5 even odd|
-------- Conversions
Use ||{0:D}| |{0:X}| |{0:N3}| |{0:P2}| (german locale!)|
to get ||123| |7B| |123,000| |12.300,00%| (german locale!)|
from |123|
Use ||{0}| |{0:U}| |{0:u}||
to get ||29.06.2012 14:50:30| |Freitag, 29. Juni 2012 12:50:30| |2012-06-29 14:50:30Z||
from |29.06.2012 14:50:30|
Use ||{0}| |{0:E1}| |{0:N1}| |{0:N2}| |{0:N3}||
to get ||1234,56| |1,2E+003| |1.234,6| |1.234,56| |1.234,560||
from |1234,56|
-------- Alignment
Use ||{0,1:D}| |{0,2:D}| |{0,-2:D}| |{0,5:D}| |{0,-5:D}||
to get ||12| |12| |12| | 12| |12 ||
from |12|
If you are interested in the test/demo script to do some experiments
of your own:
Option Explicit
' Class cFormat ...
Dim oFormat : Set oFormat = New cFormat
Dim aTests : aTests = Array( _
Array("Interpolation" _
, Array( _
Array(True, "Value1 is {0} and Value2 is {1}.", Array("zero", "one")) _
, Array(False, "{0} x 2 => {0}{0}" , "once" ) _
} _
) _
, Array("Cherrypicking" _
, Array( _
Array(True , "{6,4}: [{0}, {2}, {4}]", Array(0, 1, 2, 3, 4, 5, "even", "odd")) _
, Array(True , "{7,4}: [{5}, {3}, {1}]", Array(0, 1, 2, 3, 4, 5, "even", "odd")) _
} _
) _
, Array("Conversions" _
, Array( _
Array(False, "|{0:D}| |{0:X}| |{0:N3}| |{0:P2}| (german locale!)", 123 ) _
, Array(False, "|{0}| |{0:U}| |{0:u}|" , Now ) _
, Array(False, "|{0}| |{0:E1}| |{0:N1}| |{0:N2}| |{0:N3}|" , 1234.56 ) _
} _
) _
, Array("Alignment" _
, Array( _
Array(False, "|{0,1:D}| |{0,2:D}| |{0,-2:D}| |{0,5:D}| |{0,-5:D}|", 12 ) _
} _
) _
)
Dim sFormat : sFormat = "Use |{0}|{3}to get |{1}|{3}from |{2}|{3}"
Dim aData : aData = Array(0, 1, 2, vbCrLf)
Dim aTest
For Each aTest In aTests
WScript.Echo "--------", aTest(0)
Dim aSample
For Each aSample In aTest(1)
aData(0) = aSample(1)
If aSample(0) Then
aData(1) = oFormat.formatArray(aSample(1), aSample(2))
aData(2) = Join(aSample(2))
Else
aData(1) = oFormat.formatOne( aSample(1), aSample(2))
aData(2) = aSample(2)
End If
WScript.Echo oFormat.formatArray(sFormat, aData)
Next
WScript.Echo
Next
To learn about formatting in .NET, start with StringBuilder.AppendFormat Method (String, Object) and Formatting Types.
See here and here for ideas to include (not Copy&Paste) such a Class into your script.
Here's a nice little function that works something like the .NET string.Format function. I did this quickly so adding err handling is up to you. I did this in VB6 and added a reference to Microsoft VBScript Regular Expressions 5.5
Public Function StringFormat(ByVal SourceString As String, ParamArray Arguments() As Variant) As String
Dim objRegEx As RegExp ' regular expression object
Dim objMatch As Match ' regular expression match object
Dim strReturn As String ' the string that will be returned
Set objRegEx = New RegExp
objRegEx.Global = True
objRegEx.Pattern = "(\{)(\d)(\})"
strReturn = SourceString
For Each objMatch In objRegEx.Execute(SourceString)
strReturn = Replace(strReturn, objMatch.Value, Arguments(CInt(objMatch.SubMatches(1))))
Next objMatch
StringFormat = strReturn
End Function
Example:
StringFormat("Hello {0}. I'd like you to meet {1}. They both work for {2}. {0} has worked for {2} for 15 years.", "Bruce", "Chris", "Kyle")
Returns:
Hello Bruce. I'd like you to meet Chris. They both work for Kyle. Bruce has worked for Kyle for 15 years.
Why not? This code works here:
value1 = "1"
value2 = "2"
strFormat = "Value1 is {0} and Value2 is {1}."
strFormat = Replace (strFormat, "{0}", value1)
strFormat = Replace (strFormat, "{1}", value2)
MsgBox strFormat
Note I update my strFormat value for every replace.
If you needs a more flexible implementation, you can go with a regular expression, but doesn't seems required now.
I really liked the functionality of #Ekkehard.Horner's StringBuilder-based solution, but it seemed more complicated than necessary.
For my purposes, I definitely do not need the ceremony of a whole class.
I pared it down to this single function:
Function FormatString(format, args)
dim resultBuilder
set resultBuilder = CreateObject("System.Text.StringBuilder")
if IsArray(args) Then
resultBuilder.AppendFormat_4 format, (args)
else
resultBuilder.AppendFormat format, args
end if
FormatString = resultBuilder.ToString()
End Function
WScript.Echo FormatString("Hello, {0}!", "World")
WScript.Echo FormatString("Hello, {0}! It is {1:H:mm tt}, and we are {2:P2} through the day on {1:dddd, d MMMM, yyyy}.", Array("World", Now, Timer/(24*60*60)))
Related
I need a function to check if a string is all (or mostly) capitals using classic asp. (I need to prevent users from inputting titles using all capitals.)
For example, if a string of 30 letters contains 20 or more that are capitalized, I'd need to flag it as "All capitals". So "The Count of Monte Cristo" would be fine, but "The COUNT of MONTE CRISTO" would not.
I was thinking about starting with a count of letters that match [^A-Z], but how do I do that?
This needs to be in Classic ASP and not VB.
Comparing against UCase(input) makes it an all or nothing check; I'd prefer to look at the UCase ratio:
Option Explicit
Function Ucasity(s)
If Len(s) Then
Dim r : Set r = New RegExp
r.Global = True
r.Pattern = "[A-Z]"
Dim m : Set m = r.Execute(s)
Ucasity = m.Count / Len(s)
Else
Ucasity = 0
End If
End Function
Function qq(s) : qq = """" & s & """" : End Function
Dim s
For Each s In Array( _
"UPPERCASE but not ALL OR NOTHING" _
, "UPPERCASE" _
, "pipapo" _
, "UPPERCASEuppercase" _
, "" _
)
WScript.Echo qq(s), CStr(s = UCase(s)), UCasity(s)
Next
output:
cscript 39261181.vbs
"UPPERCASE but not ALL OR NOTHING" False 0,65625
"UPPERCASE" True 1
"pipapo" False 0
"UPPERCASEuppercase" False 0,5
"" True 0
Simply use the UCase function
<%
dim a
a = "This is a test 1"
dim b
b = "THIS IS A TEST 2"
If a = ucase(a) then response.write(a & " is all upper")
If b = ucase(b) then response.write(b & " is all upper")
%>
Result
THIS IS A TEST 2 is all upper
I would appreciate if anybody can help me with this issue I am having. Basically, the VBA is a search function that enables the user to search part of or the entire name of the job, from a job database.
However, it results in "Runtime error 7: Out of Memory." This happens only on my Macbook, and does not happen on a Windows computer. Upon clicking "debug", it brought me to this line of code:
`If scd.Cells(i, j) Like "*" & Search & "*" Then
please help! Thank you!
The rest of the code is below:
Option Compare Text
Sub SearchClientRecord()
Dim Search As String
Dim Finalrow As Integer
Dim SearchFinalRow As Integer
Dim i As Integer
Dim scs As Worksheet
Dim scd As Worksheet
Set scs = Sheets("Client Search")
Set scd = Sheets("Client Database")
scs.Range("C19:S1018").ClearContents
Search = scs.Range("C12")
Finalrow = scd.Range("D100000").End(xlUp).Row
SearchFinalRow = scs.Range("D100000").End(xlUp).Row
For j = 3 To 19
For i = 19 To Finalrow
If scd.Cells(i, j) Like "*" & Search & "*" Then
scd.Range(scd.Cells(i, 3), scd.Cells(i, 19)).Copy
scs.Range("C100000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
End If
Next i
Next j
scs.Range("C19:S1018").Select
scs.Range("$C$18:$S$1009").RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6 _
, 7), Header:=xlYes
Call Border
Columns("C:S").HorizontalAlignment = xlCenter
End Sub
I created an alternate function called "aLike" below.
In your code you would use it by saying: If aLike("*" & Search & "*",scd.Cells(i, j)) Then
I can't guarantee it works exactly the same way, but I would be interested to see if the Mac can process this function better than "like".
Function aLike(asterixString As Variant, matchString As Variant, Optional MatchCaseBoolean As Boolean) As Boolean
Dim aStr As Variant, mStr As Variant, aStrList As New Collection
Dim i As Long, aPart As Variant, mPart As Variant, TempInt As Long, aStart As Boolean, aEnd As Boolean
aStr = asterixString: mStr = matchString
If Not MatchCaseBoolean Then aStr = StrConv(aStr, vbLowerCase): mStr = StrConv(mStr, vbLowerCase)
' Get rid of excess asterix's
While InStr(aStr, "**") > 0
aStr = Replace(aStr, "**", "*")
Wend
' Deal with trivial case
If aStr = mStr Then aLike = True: GoTo EndFunction
If aStr = "*" Then aLike = True: GoTo EndFunction
If Len(aStr) = 0 Then aLike = False: GoTo EndFunction
' Convert to list
aStart = Left(aStr, 1) = "*": If aStart Then aStr = Right(aStr, Len(aStr) - 1)
aEnd = Right(aStr, 1) = "*": If aEnd Then aStr = Left(aStr, Len(aStr) - 1)
aLike_Parts aStr, aStrList
' Check beginning
If Not aStart Then
aPart = aStrList.Item(1)
If Not (aPart = Left(mStr, Len(aPart))) Then aLike = False: GoTo EndFunction
End If
' Check end
If Not aEnd Then
aPart = aStrList.Item(aStrList.Count)
If Not (aPart = Right(mStr, Len(aPart))) Then aLike = False: GoTo EndFunction
End If
' Check parts
mPart = mStr
For i = 1 To aStrList.Count
aPart = aStrList.Item(i): TempInt = InStr(mPart, aPart)
If TempInt = 0 Then aLike = False: GoTo EndFunction
mPart = Right(mPart, Len(mPart) - TempInt - Len(aPart) + 1)
If Len(mPart) = 0 And i < aStrList.Count Then aLike = False: GoTo EndFunction
Next i
aLike = True
EndFunction:
Set aStrList = Nothing
End Function
Function aLike_Parts(Str As Variant, StrList As Collection) As Variant
Dim Char As String, wPart As String
For i = 1 To Len(Str)
Char = Mid(Str, i, 1)
If Char = "*" Then
StrList.Add wPart: wPart = ""
Else
wPart = wPart & Char
End If
Next i
If Len(wPart) > 0 Then StrList.Add wPart
End Function
Good Luck!
#Alex P , now .find is NOT more efficient, for example :
Option Explicit
Option Compare Text
Sub SearchClientRecord()
With Application
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
Dim Search As String
Dim Finalrow As Long
Dim SearchFinalRow As Long
Dim i&, j&
Dim scs As Worksheet
Dim scd As Worksheet
Dim DATA() As Variant
Dim Range_to_Copy As Range
Set scs = Sheets("Client Search")
Set scd = Sheets("Client Database")
With scd
Finalrow = .Range("D100000").End(xlUp).Row
DATA = .Range(.Cells(19, 3), .Cells(Finalrow, 19)).Value2
End With
With scs
.Range("C19:S1018").ClearContents
Search = .Range("C12").Value
SearchFinalRow = .Range("D100000").End(xlUp).Row
End With
With scd
For j = 3 To 19
For i = 19 To Finalrow
If InStr(DATA(i, j), Search) > 0 Then
'If scd.Cells(i, j) Like "*" & Search & "*" Then
If Not Range_to_Copy Is Nothing Then
Set Range_to_Copy = Union(Range_to_Copy, .Range(.Cells(i, 3), .Cells(i, 19)))
'scd.Range(scd.Cells(i, 3), scd.Cells(i, 19)).Copy
'scs.Range("C100000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
Else
Set Range_to_Copy = .Range(.Cells(i, 3), .Cells(i, 19))
End If
End If
Next i
Next j
End With 'scd
Erase DATA
With scs
Range_to_Copy.Copy _
Destination:=.Range("C100000").End(xlUp).Offset(1, 0) '.PasteSpecial xlPasteFormulasAndNumberFormats
.Range("C19:S1018").Select 'this line might be superflous
.Range("$C$18:$S$1009").RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7), Header:=xlYes
End With
Call Border
Columns("C:S").HorizontalAlignment = xlCenter 'on wich worksheet ??
Set Range_to_Copy = Nothing
Set scs = Nothing
Set scd = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
why now return ....Syntax error
Can i put the Sub rutine in the function? Or better way for this?!
Function SumerizePlanArrays(f_String, f_Type)
Set dic = CreateObject("Scripting.Dictionary")
Sub Add(s)
weight = Split(s,"$")(0)
values = Split(s,"$")(1)
pipes = Split(values, "|")
For Each line In pipes
val = Split(line, ",")
if f_Type = 1 then
dic(val(1)) = (dic(val(1))*weight/100) + CInt(val(2))
elseif f_Type = 2 then
dic(val(1)) = dic(val(1)) + CInt(val(2))
end if
Next
End Sub
arrString = Split(f_String,"#")
For i = 0 to UBound(arrString)
'wei = Split(arrString(i),"$")(0)
Add arrString(i)
Next
Set a = CreateObject("System.Collections.ArrayList")
For Each key In dic.Keys
a.Add "0," & key & "," & dic(key)
Next
a.Sort
result = Join(a.ToArray, "|")
SumerizePlanArrays = result
End Function
Microsoft VBScript compilation error '800a03ea'
Syntax error
/inc_func_projects.asp, line 2592
Sub Add(s)
^
No - you can't put a sub within a function, except in JavaScript or in the server side version called JScript. VBScript and JScript are two completely different languages, however.
You should be doing this...
Function SumerizePlanArrays(f_String, f_Type)
Set dic = CreateObject("Scripting.Dictionary")
arrString = Split(f_String,"#")
For i = 0 to UBound(arrString)
'NOTE: Updated the call to reflect comment by sadrasjd...
Add arrString(i, f_Type, dic)
Next
Set a = CreateObject("System.Collections.ArrayList")
For Each key In dic.Keys
a.Add "0," & key & "," & dic(key)
Next
a.Sort
result = Join(a.ToArray, "|")
SumerizePlanArrays = result
End Function
Sub Add(s, type, dic)
'NOTE: ^Updated the parameters to reflect comment by sadrasjd^
weight = Split(s,"$")(0)
values = Split(s,"$")(1)
pipes = Split(values, "|")
For Each line In pipes
val = Split(line, ",")
if type = 1 then
dic(val(1)) = (dic(val(1))*weight/100) + CInt(val(2))
elseif type = 2 then
dic(val(1)) = dic(val(1)) + CInt(val(2))
end if
Next
End Sub
NOTE: Updated the call to reflect the suggestion made by sadrasjd.
I have a huge file containing values with a regex in it, like this:
LGP0041_\d{4}\.dta
objd135a_\S{3}.txt
Now I need to convert these to a valid example value, like this:
LGP0041_1234.dta
objd135a_abc.txt
I know of the RegExp object to check if there is a match, but is there also a way to create valid values?
A regular grammar can be used to recognize or produce words of its language, but the VBScript regexp engine does not implement producing. So you have to roll your own.
Your sample does not contain contain regular patterns. \S can't mean 'non-whitespace' because you won't like characters illegal in a file name and a representative sample of file names should contain elements with spaces. The fact that the first sample escapes the extension dot and the second one doesn't makes me think that your syntax specs aren't really thought out. If you come up with a (regular) grammar of your inputs, I'm willing to give your problem further thought.
Some code to base the thinking on:
Option Explicit
Function rndInt(lowerbound, upperbound)
rndInt = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
End Function
Sub shuffleAD(aX)
' Durstenfeld's Permutation Algorithm
Dim J, K, Temp
For J = UBound(aX) To 1 Step -1
K = Int((J + 1) * Rnd) ' random number 0 - J
Temp = aX(J)
aX(J) = aX(K)
aX(K) = Temp
Next
End Sub ' shuffleAD
Class cRGen
Private m_
Public Function init(s)
Set init = Me
ReDim m_(Len(s) - 1)
Dim i
For i = 0 To UBound(m_)
m_(i) = Mid(s, i + 1, 1)
Next
End Function
Public Function getNext(mi, ma)
shuffleAD m_
getNext = Mid(Join(m_, ""), 1, rndInt(mi, ma))
End Function
End Class
Dim goRpl : Set goRpl = Nothing
Function magic(m, w, mi, ma, p, src)
If IsEmpty(ma) Then ma = mi
magic = goRpl.m_dicGens(w).getNext(mi, ma)
End Function
Class cRpl
Private m_fRpl
Private m_r
Public m_dicGens
Private Sub Class_Initialize()
Set m_fRpl = GetRef("magic")
Set m_r = New RegExp
m_r.Pattern = "\\(\w){(\d+)(?:,(\d+))?}"
Set m_dicGens = CreateObject("Scripting.Dictionary")
Set m_dicGens("d") = New cRGen.init("0123456789")
Set m_dicGens("S") = New cRGen.init("abcdefghij")
End Sub
Public Function rpl(s)
Set goRpl = me
rpl = m_r.Replace(s, m_fRpl)
End Function
End Class
Randomize
Dim aTests : aTests = Array( _
"LGP0041_\d{4}.dta" _
, "objd135a_\S{3}.txt" _
, "x_\S{3,8}.txt" _
)
Dim oRpl : Set oRpl = New cRpl
Dim sTest
For Each sTest In aTests
WScript.Echo sTest, "=>", oRpl.rpl(sTest)
Next
output:
cscript 28936688.vbs
LGP0041_\d{4}.dta => LGP0041_4317.dta
objd135a_\S{3}.txt => objd135a_cea.txt
x_\S{3,8}.txt => x_jgcfidh.txt
cscript 28936688.vbs
LGP0041_\d{4}.dta => LGP0041_8054.dta
objd135a_\S{3}.txt => objd135a_eci.txt
x_\S{3,8}.txt => x_ahfgd.txt
This should at least identify the needed components:
generators that deliver strings of specific character sets
a (sorry: global) replace function that maps 'type letters' to generators, handles 'width specs', and build the output
a regular pattern to parse the specs from your inputs
I would like to extract a given string out of another one.
The sample strings shall be:
V_DDRF_2J_WTF
V_ASDF_8J_TLDR
V_LULZ_1337_3J
(Hint: The letter after the integer is ALWAYS a J.)
Now I only want to extract the integer of the bold part of the string. How do I achieve that?
xJ will only appear in the middle or the end of the string.
Use a RegExp searching for "", a (sequence of) number(s), and an optional "" - as in:
Option Explicit
Function qq(s) : qq = """" & s & """" : End Function
Dim aTests : aTests = Array( _
"V_DDRF_2J_WTF" , "_2J_" _
, "V_ASDF_8J_TLDR", "_8J_" _
, "V_LULZ_1337_3J", "_3J" _
)
Dim r : Set r = New RegExp
'r.Pattern = "_\d+J(?:_|$)"
r.Pattern = "_\d+J_?"
Dim i, s
For i = 0 To UBound(aTests) Step 2
s = r.Execute(aTests(i + 0))(0).Value
WScript.Echo qq(aTests(i + 0)) _
, qq(s) _
, CStr(aTests(i + 1) = s)
Next
output:
cscript 28561918.vbs
"V_DDRF_2J_WTF" "_2J_" True
"V_ASDF_8J_TLDR" "_8J_" True
"V_LULZ_1337_3J" "_3J" True
You may have to tinker with the pattern, depending on your input data; code to deal with non-matches should be added.
You can use regex and use this pattern \d+J or \d+\w
You can test patterns here