This question already has answers here:
VBScript string replace with range instead of string?
(2 answers)
Closed 2 years ago.
I want to perform the following with vbscript but not sure how to do it.
I have a fullString, and would like to replace anything after Reference, with a new string.
fullString = Reference,My Name is Bob Dillan Sharpie
searchString = Reference,
replaceString= Reference,My Name is David Naver Plus
if inStr(fullString,searchString) then
' Not sure what to do here... i only know this function.
' newString = Replace(fullString,searchString,replaceString)
end if
Some combination of Right, Len and InStr should do the trick:
fullString = "Reference,My Name is Bob Dillan Sharpie"
searchString = "Reference,"
replaceString = "My Name is David Naver Plus"
if inStr(fullString,searchString) then
newString = Replace(fullString, _
Right(fullString, Len(fullstring) - (InStr(fullString, searchString) + Len(searchString) - 1)), _
replaceString)
wscript.echo newString
end if
You can do something like
Option Explicit
On Error GoTo 0
Dim fullString, searchString, replaceString, newString, pos
If WScript.Arguments.Count = 1 Then
fullString = WScript.Arguments(0)
Else
fullString = "Reference,My Name is Bob Dillan Sharpie"
End If
searchString = "Reference,"
replaceString= "Reference,My Name is David Naver Plus"
pos = inStr( 1, fullString, searchString, vbTextCompare)
If pos > 0 Then
newString = Left( fullString, pos -1) & replaceString
Else
newString = fullString
End If
Wscript.Echo fullString & vbNewLine & newString
Output:
cscript D:\bat\SO\65346768.vbs
Reference,My Name is Bob Dillan Sharpie
Reference,My Name is David Naver Plus
cscript D:\bat\SO\65346768.vbs "Another reference,My Name is Josef Z"
Another reference,My Name is Josef Z
Another Reference,My Name is David Naver Plus
cscript D:\bat\SO\65346768.vbs "My Name is Josef Z"
My Name is Josef Z
My Name is Josef Z
Related
This question already has an answer here:
splitting string and putting each output into a unique variable in vbs
(1 answer)
Closed 5 months ago.
I am very new to programming and I am writing some code to work with Nicelabel 5.0 to display small pictures of allergens on a product label when the allergen is present within a string. I thought I really nailed it by using inStr to see if certain strings were present. I was even able to make the program distinguish between "FISH" and "SHELLFISH"
if inStr (1, Allergens1, "FISH") > 0 then
if inStr (1, Allergens1, "SHELLFISH") > 0 then
a = label.SetObjectVisible("SHELLFISH", true)
else
a = label.SetObjectVisible("FISH", true)
end if
end if
Where I'm stuck is what do I do if a product has BOTH shellfish and fish in it? The string "FISH" will always appear in the string "SHELLFISH" so how can I distinguish between the two?
Please let me know if more information is needed. The variable allergens1 is just a simple string ex.
"CONTAINS: EGG, FISH(SALMON), SHELLFISH(OYSTERS)"
I need to work within the confines of the labeling software which actually uses VBScript. I'm sorry for any confusion.
THANK YOU TO EVERYONE WHO HELPED WITH THIS! The final working code is below and should be noted will work with older version of Nicelabel that still use VBScript for label functions:
'''
Private Function GetWords(sText)
GetWords =
Split(Trim(Replace(Replace(Replace(Replace(Replace(Replace(sText, ":", " "), ",", " "), "(", " "), ")", " "), " ", " "), " ", " ")))
End Function
Dim vElem
dim a
a = label.SetObjectVisible("TREENUT",false)
a = label.SetObjectVisible("EGG",false)
a = label.SetObjectVisible("SOY", false)
a = label.SetObjectVisible("WHEAT", false)
a = label.SetObjectVisible("NUT", false)
a = label.SetObjectVisible("SHELLFISH", false)
a = label.SetObjectVisible("FISH", false)
a = label.SetObjectVisible("MILK", false)
a = label.SetObjectVisible("SESAME", false)
a = label.SetObjectVisible("SULFITE", false)
For Each vElem In GetWords(Allergens1)
a = label.SetObjectVisible(vElem,True)
Next
'''
I'm pretty new myself so grain of salt and whatnot, but I think that first I'd split the string into several strings using the comma as a separator and making it a list of ingredients that you can then use your if statement on
A simple substring search over the whole string will not suffice, as you have discovered. You need to "tokenize" your string, ie split the string:
"EGG, FISH(SALMON), SHELLFISH(OYSTERS)"
into separate substrings:
"EGG"
"FISH"
"SALMON"
"SHELLFISH"
"OYSTERS"
And then you can iterate and compare each substring as-is as needed.
The approach I would take, as mentioned by several people, is to tokenize your string and then have your display logic use that list of tokens. Something like the following:
Dim Description
Dim Allergens
Description = "CONTAINS: EGG, FISH(SALMON)"
WScript.Echo Description
Set Allergens = GetAllergens(Description)
DisplayAllergens Allergens
WScript.Echo ""
Description = "CONTAINS: EGG, SHELLFISH(OYSTERS)"
WScript.Echo Description
Set Allergens = GetAllergens(Description)
DisplayAllergens Allergens
WScript.Echo ""
Description = "CONTAINS: EGG, FISH(SALMON), SHELLFISH(OYSTERS)"
WScript.Echo Description
Set Allergens = GetAllergens(Description)
DisplayAllergens Allergens
WScript.Echo ""
Private Function GetAllergens(ByVal Description)
Dim Allergens
Dim Allergen
Dim i
Set GetAllergens = CreateObject("Scripting.Dictionary")
Description = Replace(Description, "CONTAINS: ", "")
Allergens = Split(Description, ",")
For i = LBound(Allergens) To UBound(Allergens)
Allergen = Trim(Split(Allergens(i), "(")(0))
GetAllergens.Add Allergen, Allergen
Next
End Function
Private Sub DisplayAllergens(ByVal Allergens)
Dim Allergen
For Each Allergen In Allergens
If Allergen = "EGG" Then WScript.Echo "EGG picture"
If Allergen = "SHELLFISH" Then WScript.Echo "SHELLFISH picture"
If Allergen = "FISH" Then WScript.Echo "FISH picture"
Next
End Sub
Here is the result:
Here is a one-liner which splits your string into words:
Private Function GetWords(sText)
GetWords = Split(Trim(Replace(Replace(Replace(Replace(Replace(Replace(sText, ":", " "), ",", " "), "(", " "), ")", " "), " ", " "), " ", " ")))
End Function
You can use this with For Each like this:
Dim Allergens1
Dim vElem
Allergens1 = "CONTAINS: EGG, FISH(SALMON), SHELLFISH(OYSTERS)"
For Each vElem In GetWords(Allergens1)
WScript.echo vElem & " picture"
Next
. . . to get this:
CONTAINS picture
EGG picture
FISH picture
SALMON picture
SHELLFISH picture
OYSTERS picture
hi all i have this question as bellow
how capitalize full in one vb6 Vb6 string variable
‘example
‘my fullname
Dim fullname as string
Fullname = “abdirahman abdirisaq ali”
Msgbox capitalize(fullname)
it prints abdirahmanAbdirisaq ali that means it skips the middle name space even if I add more spaces its same .
this is my own code and efforts it takes me at least 2 hours and still .
I tired it tired tired please save me thanks more.
Please check my code and help me what is type of mistakes I wrote .
This is my code
Private Function capitalize(txt As String) As String
txt = LTrim(txt)
temp_str = ""
Start_From = 1
spacing = 0
For i = 1 To Len(txt)
If i = 1 Then
temp_str = UCase(Left(txt, i))
Else
Start_From = Start_From + 1
If Mid(txt, i, 1) = " " Then
Start_From = i
spacing = spacing + 1
temp_str = temp_str & UCase(Mid(txt, Start_From + 1, 1))
Start_From = Start_From + 1
Else
temp_str = temp_str & LCase(Mid(txt, Start_From, 1))
End If
End If
Next i
checkName = temp_str
End Function
It's far simpler than that. In VB6 you should use Option Explicit to properly type your variables. That also requires you to declare them.
Option Explicit
Private Function capitalize(txt As String) As String
Dim temp_str as String
Dim Names As Variant
Dim Index As Long
'Remove leading and trailing spaces
temp_str = Trim$(txt)
'Remove any duplicate spaces just to be sure.
Do While Instr(temp_str, " ") > 0
temp_str = Replace(temp_str, " ", " ")
Loop
'Create an array of the individual names, separating them by the space delimiter
Names = Split(temp_str, " ")
'Now put them, back together with capitalisation
temp_str = vbnullstring
For Index = 0 to Ubound(Names)
temp_str = temp_str + Ucase$(Left$(Names(Index),1)) + Mid$(Names(Index),2) + " "
Next
'Remove trailing space
capitalize = Left$(temp_str, Len(temp_str) - 1)
End Function
That's the fairly easy part. If you are only going to handle people's names it still needs more work to handle names like MacFarland, O'Connor, etc.
Business names get more complicated with since they can have a name like "Village on the Lake Apartments" where some words are not capitalized. It's a legal business name so the capitalization is important.
Professional and business suffixes can also be problematic if everything is in lower case - like phd should be PhD, llc should be LLC, and iii, as in John Smith III, would come out Iii.
There is also a VB6 function that will capitalize the first letter of each word. It is StrConv(string,vbProperCase) but it also sets everything that is not the first letter to lower case. So PhD becomes Phd and III becomes Iii. Where as the above code does not change the trailing portion to lower case so if it is entered correctly it remains correct.
Try this
Option Explicit
Private Sub Form_Load()
MsgBox capitalize("abdirahman abdirisaq ali")
MsgBox capitalize("abdirahman abdirisaq ali")
End Sub
Private Function capitalize(txt As String) As String
Dim Names() As String
Dim NewNames() As String
Dim i As Integer
Dim j As Integer
Names = Split(txt, " ")
j = 0
For i = 0 To UBound(Names)
If Names(i) <> "" Then
Mid(Names(i), 1, 1) = UCase(Left(Names(i), 1))
ReDim Preserve NewNames(j)
NewNames(j) = Names(i)
j = j + 1
End If
Next
capitalize = Join(NewNames, " ")
End Function
Use the VB6 statement
Names = StrConv(Names, vbProperCase)
it's all you need (use your own variable instead of Names)
How can I write my full name where the first letter is in upper-case and the rest is lower-case for example:
Michael Jonson Bech
I have this so fair:
option Explicit
Dim Name,fName
Name = Split(InputBox("what is your name"))
Dim var
For Each var In Name
'var=UCase(Left(var,1))
LCase(var)
UCase (Left(var,1))
Next
fName = Join(Name)
WScript.Echo("you name is : " & fName )
String functions like UCase do not modify the operand, but return a modified copy. For Each v gives you copies of the array's elements named v.
So you need something like this:
Option Explicit
Dim a : a = Split("mIchael jOnson bEch")
WScript.Echo Join(a)
Dim i
For i = 0 To UBound(a)
a(i) = UCase(Left(a(i), 1)) & LCase(Mid(a(i), 2))
Next
WScript.Echo Join(a)
output:
cscript 34629546.vbs
mIchael jOnson bEch
Michael Jonson Bech
This looks like VB6, in which case something like :
Dim Name as string
Name = InputBox("what is your name")
Name = StrConv(Name, vbProperCase)
I'm trying to retrieve the 5 characters before and after the string "Major-General" in the Pirate's of Penzance Major-General Song (found here: http://www.naic.edu/~gibson/poems/gilbert1.html ). I'm trying to find a better way to do this than what I have and also trying to figure out why it isn't looping. Any ideas would be greatly appreciated!
l1 =INSTR(l2, string, "Major-General")
l2 = 5
l3 = 1
vcount=0
if vcount <5 then
l1 =INSTR(l3, string, "Major-General")
vcount = vcount +1
word = mid(string, l1-5, l2 )
word1 = mid(string, l1+13, l2)
l3 = l3+l1
response.write "<br>" & "5 before: " & word & "<br>" & "5 after: " & word1
end if
It may be easier to do it like this:
Dim Poem,aStr,i,block,Left5,Right5
Poem = "The abc poem is abc in this abc string."
aStr = Split(Poem,"abc")
If uBound(aStr) = 0 Then Response.Write "String not found": Response.End
For i = 0 to uBound(aStr)
block = Trim(aStr(i))
Left5 = "": Right5 = ""
if cStr(i) <> cStr(uBound(aStr)) then Left5 = Right(block,5)
If cStr(i) <> cStr(0) then Right5 = Left(block,5)
Response.Write "The 5 characters to the left of abc:" & Left5
Response.Write "The 5 characters to the right of abc:" & Right5
Next
This will get the 5 characters to the left and right of the string, each time the string exists.
In this case, it will search the Poem string for abc and split the string into an array of each part where the string exists, then it can be processed further.
Suppose an Excel sheet has a column named Student Names and the column has duplicate values. Say,
Student
=======
Arup
John
Mike
John
Lisa
Arup
Using VBScript, how can I get unique values as below?
Arup
John
Lisa
Mike
The VBScript tool for getting unique items is a dictionary: add all the items as keys to a dictionary and dictionary.Keys() will return an array of the - per definitionem - unique keys. In code:
Dim aStudents : aStudents = Array("Arup", "John", "Mike", "John", "Lisa", "Arup")
WScript.Echo Join(aStudents)
Dim aUniqStudents : aUniqStudents = uniqFE(aStudents)
WScript.Echo Join(aUniqStudents)
' returns an array of the unique items in for-each-able collection fex
Function uniqFE(fex)
Dim dicTemp : Set dicTemp = CreateObject("Scripting.Dictionary")
Dim xItem
For Each xItem In fex
dicTemp(xItem) = 0
Next
uniqFE = dicTemp.Keys()
End Function
output:
Arup John Mike John Lisa Arup
Arup John Mike Lisa
Alternatively, since your source data is in an Excel spreadsheet, you could use SQL to fill an ADODB Recordset:
Option Explicit
Dim filePath, sheetName
filePath = "C:\path\to\excel\workbook.xlsx"
sheetName = "Sheet1"
Dim connectionString
connectionString = _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=""" & filePath & """;" & _
"Extended Properties=""Excel 12.0;HDR=No"""
Dim sql
sql = "SELECT DISTINCT Student FROM [" & sheetName & "$]"
Dim rs
Set rs = CreateObject("ADODB.Recordset")
rs.Open sql, connectionString
Do Until rs.EOF
WScript.Echo rs("StudentName")
Loop
If you don't want a Dictionary you can use the following to compare each element in the array to itself.
Info = Array("Arup","John","Mike","John","Lisa","Arup")
x = 0
z = ubound(Info)
Do
x = x + 1
Do
z = z - 1
If x = z Then
Info(x) = Info(z)
ElseIf Info(x) = Info(z) Then
Info(x) = ""
End If
Loop Until z=0
z = ubound(Info)
Loop Until x = ubound(Info)
For each x in Info
If x <> "" Then
Unique = Unique & Chr(13) & x
End If
Next
MsgBox Unique