Check if string "starts with" another string - vbscript

I want to check if an address starts with http://www.youtube.com.
If I have something like this
if rs("mainVideoName")="http://www.youtube.com*" then
This doesn't work, so how can I do it?

Try this:
Function UrlStartsWith(string1, string2)
UrlStartsWith = InStr(1, string1, string2, 1) = 1
End Function
If UrlStartsWith(rs("mainVideoName"), "http://www.youtube.com") Then
End If
Starts with is tested by using IntStr and ensuring that it returns 1 as the starting position that the search string is found. Since you are testing a URL the code above uses a TextCompare to make insensitive to case.

You can use the InStr() function for this:
Dim positionOfMatchedString
positionOfMatchedString= InStr(rs("mainVideoName"),"http://www.youtube.com")
If positionOfMatchedString > 0 Then
// Do your stuff here
End If
As Anthony points out, this tells you that string2 is contained in string1. You could write it as:
If positionOfMatchedString = 1 Then
to check for it beginning with.

How about...
Dim s: s = "http://www.youtube.com"
Dim l: l = Len(s)
If Left(rs("mainVideoName"), l) = s Then
' String begins with URL part '
End If

Related

How do I extract numbers from a string in VB6?

I have a string that looks something like 'NS-BATHROOMS 04288'
I only want the numbers.
I hve searched for answers, but none so far even get pst the compiler.
How can I do this?
without regex you can do it with: (altough VB6/VBA Code really isn`t nice to look at)
Public Function ReturnNonAlpha(ByVal sString As String) As String
Dim i As Integer
For i = 1 To Len(sString)
If Mid(sString, i, 1) Like "[0-9]" Then
ReturnNonAlpha = ReturnNonAlpha + Mid(sString, i, 1)
End If
Next i
End Function
I'd personally use regex. You can match given regex patterns to achieve what you need. This function matches only digits.
For VB6 you'd do something like:
Dim myRegExp, ResultString
Set myRegExp = New RegExp
myRegExp.Global = True
myRegExp.Pattern = "[\d]"
Then you'd go against your String.
https://support.microsoft.com/en-us/kb/818802
You can use this function for extract numerical chr as string value:
Public Function Str_To_Int(MyString As Variant) As Long
Dim i As Integer
Dim X As Variant
If IsNull(MyString) Or MyString = "" Then
Str_To_Int = 0
Exit Function
End If
For i = 1 To Len(MyString)
If IsNumeric(Mid(MyString, i, 1)) = True Then
X = Nz(X, "") & Mid(MyString, i, 1)
End If
Next i
Str_To_Int = Nz(X, 0)
End Function

How to make a function with two strings as arguments

I'm not entirely sure what I'm doing wrong here. I have tested the code by input and output and it functions as desired no pun intended. :'P
I'm just not setting up this function correctly and I believe it's because my arguments happen to be desirably a string. Where if done correctly "CD" will be inserted into the middle of "ABEF". So, how do I go about doing this?
Thanks!
insertstr(ABEF, CD)
Function insertstr(string1, string2)
nostrmsg = "No string"
fullng = len(string1)
half = len(string1)/2
if half>0 then hfstr1 = mid(string1, 1, half)
str2lng = len(string2)
if str2lng>0 then paste = hfstr1+string2
lshalf = mid(string1, half+1, fullng)
if str2lng+half=str2lng+half then insert = paste+lshalf
End Function
Start with the knowledge that a functions returns a value, a tentative specification of what the function should do, and a basic testing skeleton:
Option Explicit
' returns the string build by inserting m(iddle) into f(ull) at half position
Function insertInto(f, m)
insertInto = "?"
End Function
Dim t, r
For Each t In Array( _
Array("ABEF", "CD", "ABCDEF") _
)
r = insertInto(t(0), t(1))
WScript.Echo t(0), t(1), r, CStr(r = t(2))
Next
output:
cscript 26873276.vbs
ABEF CD ? False
Then learn about Left, Mid, Len, and \ (integer division).
At last, re-write insertInto() so that the result starts with
cscript 26873276.vbs
ABEF CD ABCDEF True

How to get the index of a String after the first space in vbsript

I'm trying to grab the first characters before a space.
I know it can be done this way
str = "3 Hello World"
str = Mid(str, 1,2)
But how would i do this after a space?
Edit: Looks like you changed your question to get characters BEFORE the first space instead of AFTER. I've updated my examples.
Here's one way:
strTextBeforeFirstSpace = Split(str, " ")(0)
Assuming a space exists in your string, this would return everything up until the first space.
Another way would be:
strTextBeforeFirstSpace = Left(str, InStr(str, " ") - 1)
You can get the index of the first space with the InStr function
InStr(str, " ")
And use this as a parameter in your Mid function
Dim str, index
str = "3 Hello World"
index = InStr(str," ")
'only neccessary if there is a space
If index > 0 Then
str = Mid(str,1,index - 1)
End If

Remove the desired content from a text

I would like to get a working code to simply remove from a text line a specific part that always begins with "(" and finish with ")".
Sample text : Hello, how are you (it is a question)
I want to remove this part: "(it is a question)" to only keep this message "Hello, how are you"
Lost...
Thanks
One way using Regular Expressions;
input = "Hello, how are you (it is a question)"
dim re: set re = new regexp
with re
.pattern = "\(.*\)\s?" '//anything between () and if present 1 following whitespace
.global = true
input = re.Replace(input, "")
end with
msgbox input
If the part to be removed is always at the end of the string, string operations would work as well:
msg = "Hello, how are you (it is a question)"
pos = InStr(msg, "(")
If pos > 0 Then WScript.Echo Trim(Left(msg, pos-1))
If the sentence always ends with the ( ) section, use the split function:
line = "Hello, how are you (it is a question)"
splitter = split(line,"(") 'splitting the line into 2 sections, using ( as the divider
endStr = splitter(0) 'first section is index 0
MsgBox endStr 'Hello, how are you
If it is in the middle of the sentence, use the split function twice:
line = "Hello, how are you (it is a question) and further on"
splitter = split(line,"(")
strFirst = splitter(0) 'Hello, how are you
splitter1 = split(line,")")
strSecond = splitter1(UBound(Splitter1)) 'and further on
MsgBox strFirst & strSecond 'Hello, how are you and further on
If there is only one instance of "( )" then you could use a '1' in place of the UBound.
Multiple instances I would split the sentence and then break down each section containing the "( )" and concatenate the final sentence.

how do you parse a string in vb6?

Some of us unfortunately are still supporting legacy app like VB6. I have forgotten how to parse a string.
Given a string:
Dim mystring As String = "1234567890"
How do you loop in VB6 through each character and do something like
for each character in mystring
debug.print character
next
In C# i would do something like
char[] myChars = mystring.ToCharArray();
foreach (char c in theChars)
{
//do something with c
}
Any ideas?
Thanks a lot
You can use the 'Mid' function to get at the individual characters:
Dim i As Integer
For i = 1 To Len(mystring)
Print Mid$(mystring, i, 1)
Next
Note this is untested.
There is no possibility to use foreach on strings.
Use
Dim i As Integer
For i = 1 To Len(YourString)
Result = Mid$(YourString, i, 1)
Next
note that the type of Result is a length-1 string, no char or byte type.
If performance is important, you'll have to convert the string to a bytearray fist (using StrConv) and then loop through it like this.
Dim i As Long
For i = 0 To UBound(Data)
Result = Data(i) ' Type is Byte '
Next
This is much more efficient.
The easiest way is to convert the string into an array of bytes and iterate over the byte array (converting each byte to a character).
Dim str As String
Dim bytArray() As Byte
Dim count As Integer
str = "This is a string."
bytArray = str
For count = 0 To UBound(bytArray)
Debug.Print Chr(bytArray(count))
Next
Don't loop; rather, set a reference to Microsoft VBScript Regular Expressions library and use regular expressions to achieve your 'do something' goal.

Resources