Splitting the Name and Changing the First Letter to Upper Case - vbscript

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)

Related

Count Items in file using VB

Kind of new to VBS. I'm trying to count the fields in the file and have this code.
Col()
Function Col()
Const FSpec = "C:\test.txt"
Const del = ","
dim fs : Set fs = CreateObject("Scripting.FileSystemObject")
dim f : Set f = fs.OpenTextFile(FSpec, 1)
Dim L, C
Do Until f.AtEndOfStream
L = f.ReadLine()
C = UBound(Split(L, del))
C = C +1
WScript.Echo "Items:", C
Loop
f.Close
End Function
It works however, I don't want to count the delim inside " ".
Here's file content:
1,"2,999",3
So basically, I'm getting 4 items for now but I wanted to get 3. Kind of stuck here.
For an example of my second suggestion, a very simple example could be something like this. Not saying it is perfect, but it illustrates the idea:
Dim WeAreInsideQuotes 'global flag
Function RemoveQuotedCommas(ByVal line)
Dim i
Dim result
Dim current
For i = 1 To Len(line)
current = Mid(line, i, 1) 'examine character
'check if we encountered a quote
If current = Chr(34) Then
WeAreInsideQuotes = Not WeAreInsideQuotes 'toggle flag
End If
'process the character
If Not (current = Chr(44) And WeAreInsideQuotes) Then 'skip if comma and insode quotes
result = result & current
End If
Next
RemoveQuotedCommas = result
End Function

How capitalize fullname in vb6

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 to determine at runtime which function to call in vbscript

The range of input values possible is quite huge in my case. So a select-case methodology will not work.
So based on my input say flowers.. doffodil,lily,rose,etc, my function name to call would be flowerdoffodil(), flowerlily(), flowerrose(), etc. Ech of these functions are already defined.
Only which one to call will need to be determined at runtime based on my input.
Is there a way to do this in vb script?
Note: I am a rookie programmer and am using QTP for automation.
Use GetRef() as in:
Option Explicit
Function flowerdoffodil()
flowerdoffodil = "my name is daffodil!"
End Function
Function flowerlily()
flowerlily = "my name is lily!"
End Function
Function flowerrose()
flowerrose = "my name is rose!"
End Function
Dim aInp : aInp = Split("doffodil lily rose")
Dim sFnc
For Each sFnc In aInp
Dim fncX : Set fncX = GetRef("flower" & sFnc)
Dim sRes : sRes = fncX()
WScript.Echo sFnc, TypeName(fncX), sRes
Next
output:
cscript 30161364.vbs
doffodil Object my name is daffodil!
lily Object my name is lily!
rose Object my name is rose!
Further food for thought: You can use a Dictionary:
Dim dicFncs : Set dicFncs = CreateObject("Scripting.Dictionary")
Set dicFncs("doffodil") = GetRef("flowerdoffodil")
Set dicFncs("lily") = GetRef("flowerlily")
Set dicFncs("rose") = GetRef("flowerrose")
and call the function(s) like:
Dim sRes : sRes = dicFncs(sFnc)()
(See also: 1, 2, 3)

How to extract a given string out of another?

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

How to get only a filename?

Using VB6
Code.
Dim posn As Integer, i As Integer
Dim fName As String
posn = 0
For i = 1 To Len(flname)
If (Mid(flname, i, 1) = "\") Then posn = i
Next i
fName = Right(flname, Len(flname) - posn)
posn = InStr(fName, ".")
If posn <> 0 Then
fName = Left(fName, posn - 1)
End If
GetFileName = fName
FileName: Clockings8.mis06042009 120442.fin
But it is showing a filename is “Clockings8”. It should show “Clockings8.mis06042009 120442”
How to modify a code?
Need vb6 code Help
It is a bit cleaner to use the Scripting.FileSystemObject component. Try:
Dim fso as New Scripting.FileSystemObject
GetFileName = fso.GetBaseName(fname)
The reason why your code stops short is that InStr works from the beginning of the string to the end and stops wherever it finds a match. The filename "Clockings8.mis06042009 120442.fin" contains two periods. For this reason, you should use InStrRev instead to start the search from the end of the string.
Going with FileSystemObject's GetBaseName like David suggests is a good idea. If you can't or don't want to (and there are reasons why you might not want to) work with the FileSystemObject there's a simple solution: Remove all characters from a filename string starting with the last dot in the name.
Here's what I mean:
Dim fn As String
fn = "Clockings8.mis06042009 120442.fin"
Dim idx As Integer
idx = InStrRev(fn, ".")
GetFileName = Mid(fn, 1, idx - 1)
If your filename does not have an extenstion but has a dot somewhere in the filename string, then this method will return bad results.

Resources