Get each character in a string using VBScript - vbscript

Is there any way by which we can get each character from a string using VBScript? I had used the Mid function but I just want to know if there are any other direct functions which when used returns each character starting from a string.

strString = "test"
For i=1 To Len(strString)
WScript.Echo Mid(strString,i,1)
Next

a="abcd"
for i=1 to len(a)
msgbox right(left(a,i),1)
next

AFAIK, Mid is the only way to do this.

Another way to do it, starting from 0 :
str = "hola che"
x=Len(str)
text = ""
For i=0 to x-1 'x-1 is because it exceeds the actual length
text= text & Mid(str,i+1,1)
Next
msgbox text

This code is useful to split Ucase and Lcase
Dim a
a="StAcKoVeRfLoW"
for i=o to len(a)-1
if mid(a,i+1,1)=ucase(mid(a,i+1,1)) then
b=mid(a,i+1,1)
msgbox b
end if
next

This works for me. LEFT and then RIGHT....
'Ugandan National Identity Number (NIN) has 14 digits
strFullNIN = "18650929392010"
strNIN_1 = LEFT(strFullNIN,1)
strNIN_2 = RIGHT(LEFT(strFullNIN,2),1)
strNIN_3 = RIGHT(LEFT(strFullNIN,3),1)
strNIN_4 = RIGHT(LEFT(strFullNIN,4),1)
strNIN_5 = RIGHT(LEFT(strFullNIN,5),1)
strNIN_6 = RIGHT(LEFT(strFullNIN,6),1)
strNIN_7 = RIGHT(LEFT(strFullNIN,7),1)
strNIN_8 = RIGHT(LEFT(strFullNIN,8),1)
strNIN_9 = RIGHT(LEFT(strFullNIN,9),1)
strNIN_10 = RIGHT(LEFT(strFullNIN,10),1)
strNIN_11 = RIGHT(LEFT(strFullNIN,11),1)
strNIN_12 = RIGHT(LEFT(strFullNIN,12),1)
strNIN_13 = RIGHT(LEFT(strFullNIN,13),1)
strNIN_14 = RIGHT(LEFT(strFullNIN,14),1)
If I didn't know the length of the initial string, I would do as follows:
strFullNIN = RS.fields("Client_NIN")
strFullNIN_LENGTH = LEN(strFullNIN)
x = 1
DO UNTIL x = strFullNIN_LENGTH
IF x = 1 THEN
strNIN_"& x &" = LEFT(strFullNIN,x)
ELSE
strNIN_"& x &" = RIGHT(LEFT(strFullNIN,x),1)
END IF
x=x+1
LOOP
Hope this is helpful to someone!

Related

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)

PC Excel macros doesn't work on Mac

I wrote a macros that removes all the non-numeric characters from the logistics code and multiplies numbers left. On the pc it's okey, on my mac it doesn't work.
Here the code, probably someone could suggest me why the macros doesn't work on mac:
Function Bek(s)
Static re As Object
Dim x
If re Is Nothing Then
Set re = CreateObject("vbscript.regexp")
re.Pattern = "[0-9.,]+"
re.Global = True
End If
If re.test(s) Then
Bek = 1
For Each x In re.Execute(s)
Bek = Bek * Val(Replace(x, ",", "."))
Next
End If
End Function
I could also attach a file upon your request.
P.S. found some information, that the regexp might not be supported by mac os.
Very appreciate your help.
Try this.
It splits and looks at each part of the string and handles it the way I think you want.
Try it and see if it works
Sub ApplesWayOfRegex()
Str1 = "S10 2000*6000"
ReDim chars(Len(Str1) - 1)
For i = 1 To Len(Str1)
If (IsNumeric(Mid$(Str1, i, 1))) And i <> 1 Then
chars(i - 1) = Mid$(Str1, i, 1)
ElseIf (Not IsNumeric(Mid$(Str1, i, 1))) And i = 1 Then
chars(i - 1) = ""
Else
chars(i - 1) = "*"
End If
Next
Mathstr = Join(chars, "")
MsgBox Application.Evaluate(Mathstr)
End Sub

Find and Find Next for RichTextBox

richTextBox1.Text = "Where there is a will there is way";
I just want to change the both is only red color.
I know how to change the first is, but i don't know how to change the second is.
RichTextBox1.SelStart = RichTextBox1.Find("is")
RichTextBox1.SelLength = 2
RichTextBox1.SelColor = vbRed
According to the MSDN Article:
If the text searched for is found, the Find method highlights the
specified text and returns the index of the first character
highlighted. If the specified text is not found, the Find method
returns 1.
I'm assuming it's a typo and the return is -1 rather than 1 if the text isn't found, so, in your code:
Dim idx As Integer
Dim start As Integer
Do
idx = RichTextBox1.Find("is", start) '// First time through start at beginning
If idx = -1 Then Exit Do
RichTextBox1.SelStart = idx
RichTextBox1.SelLength = 2
RichTextBox1.SelColor = vbRed
start = idx + 1 '// Set the start for .Find to the next character
Loop
RichTextBox1.SelLength = 0 ' Clear the selection

how to remove leading zeroes from a string

I have to extract the integer value from a string.
Its actually an amount field.
Say string can be 000000000000512 or 0000040000000
I want only the integer value from this string i.e.; 512/ 40000000
Please help with this in VB scripting
CInt("000000000000512")
See conversion functions: http://msdn.microsoft.com/en-us/library/s2dy91zy.aspx
Use Clng if you expect to have large numbers, as already pointed out in a comment:
Clng("000000004000512")
otherwise you'll have an overflow, as variant's subtype int is 16 bit in vbscript
This will work even with a crazy long number
Function RemoveLeadingZeroes(ByVal str)
Dim tempStr
tempStr = str
While Left(tempStr,1) = "0" AND tempStr <> ""
tempStr = Right(tempStr,Len(tempStr)-1)
Wend
RemoveLeadingZeroes = tempStr
End Function
strNewFileName = RemoveLeadingZeroes("0009283479283749823749872392384")
Use the Absolute Value of the number.
http://ss64.com/vb/abs.html
Var = ABS(Var)
I've used this technique before:
replace the zeros with spaces
left trim
replace the spaces with zeros
Replace(LTrim(Replace(str, "0", " ")), " ", "0")
Note, this doesn't work if str has meaningful spaces in it.
Function TrimLeadingZeros(value)
TrimLeadingZeros = value
while left(TrimLeadingZeros, 1) = "0" and TrimLeadingZeros <> "0"
TrimLeadingZeros = mid(TrimLeadingZeros, 2)
wend
End Function
or
Function TrimLeadingZeros(value)
dim i
i = 1
while i < len(value) and mid(value,i,1) = "0"
i = i + 1
wend
TrimLeadingZeros = mid(value, i)
End Function
Using regex:
Regex.Replace("000000000000512", "^0+", "") ' returns "512"
Regex.Replace("0000040000000", "^0+", "") ' returns "40000000"
In case your string includes digits and characters, use a Do While statement:
string = "00000456ABC"
Do While Left(string, 1) = "0"
string = Right(string, (Len(string)-1))
Loop
Function TrimLZ(str)
If Left(str, 1) = "0" Then
TrimLZ = TrimLZ(Mid(str, 2, Len(str)))
Else
TrimLZ = str
End If
End Function

What is the best vbscript code to add decimal places to all numbers in a string?

Example
G76 I0.4779 J270 K7 C90
X20 Y30
If a number begins with I J K C X Y and it doesn't have a decimal then add decimal.
Above example should look like:
G76 I0.4779 J270 K7. C90.
X20. Y30.
Purpose of this code is to convert CNC code for an older Fanuc OPC controller
Set RegEx = New RegExp
RegEx.Global = True
RegEx.Pattern = "([IJKCXY]\d+)([^\.]|$)"
newVar = RegEx.Replace (oldString, "$1.$2")
Where oldString is the original string, and newVar is the string with the decimals added.
function convert(str)
Set RegEx = New RegExp
RegEx.Global = True
RegEx.Pattern = "([IJKCXY]\d*\.?\d*)"
Set Matches = regEx.Execute(str)
For Each Match in Matches
if instr(Match.value, ".") = 0 then
str = Replace(str, Match.value, Match.value & ".")
end if
Next
convert = str
end function
tloach still answer doesn't work
Waynes works but also puts a . after every occurrence of IJKCXY
I changed if instr(Match.value, ".") = 0 then
To be like if instr(Match.value, ".") = 0 and len(Match.value) > 1 then

Resources