What is the syntax to return string a certain number of times?

String(number, character)
‘number – number of times value will be returned; mandatory parameter
‘character – character value that will be repeated; mandatory parameter
‘examples
Dim name As String
name = "Steve Musk!"
MsgBox (String(1, "S")) 'S
MsgBox (String(20, "!")) '!!!!!!!!!!!!!!!!!!!!
MsgBox (String(5, "M")) 'MMMMM

 

What is the syntax to compare strings?

StrComp (string1, string2, compare)
‘both string1, string2 are mandatory parameters
‘compare – 0 for binary comparison or 1 for textual comparison; optional parameter with 0 being default
‘please note, an integer value is returned

‘If str1 < str2 then -1
'If str1 > str2 then 1
'If str1 = str2 then 0
‘examples
MsgBox (StrComp("Hello", "Hello")) '0
MsgBox (StrComp("hello", "HELLO")) '1
MsgBox (StrComp("HELLO", "hello")) '-1
MsgBox (StrComp("Hello", "Goodbye")) '-1

 

What is the syntax to replace a string with another?

Replace(String, find, replacewith, start, count, compare)
'string - string that will be searched; mandatory parameter
'find - string part that will be replaced; mandatory parameter
'replacewith - string part that will be inserted; mandatory parameter
'start – integer specifying the starting position of search; optional parameter where default is 1
'count - integer specifying number of times the replacement has to be performed; optional parameter
'compare - integer specifying the comparison method: 0 for binary, 1 for textual; optional parameter
'examples
Dim sentence As String
sentence = "The red fox"
MsgBox (Replace(sentence, "red", "blue", 1, 1)) 'The blue fox
MsgBox (Replace(sentence, "fox", "panda", 1)) 'The red panda
MsgBox (Replace(sentence, "fox", "panda", 9)) 'panda
MsgBox (Replace(sentence, "The", "My")) 'My red fox

 

What is the syntax to split a string from the middle?

Mid (String, start, length)
'String - string from which return value is extracted; mandatory parameter
'start - integer of starting position of extraction; mandatory parameter
'length - length of extraction; optional
'examples
Dim sentence As String
sentence = "blackgraywhite"
MsgBox (Mid(sentence, 6, 4)) 'gray
MsgBox (Mid(sentence, 1, 5)) 'black
MsgBox (Mid(sentence, 10)) 'white

 

What is the syntax to split a string from the right?

Right(String, Length)
‘string – string to be searched; mandatory parameter
‘length – integer specifying the number of characters to be returned; mandatory parameter
'examples
Dim name As String
name = "Elon Gates"
MsgBox (Right(name, 5)) 'Gates
MsgBox (Right(name, 8)) 'on Gates
MsgBox (Right(name, 10)) 'Elon Gates

 

What is the syntax to split a string from the left?

Left(String, Length)
‘string – string to be searched; mandatory parameter
‘length – integer specifying the number of characters to be returned; mandatory parameter
'examples
Dim name As String
name = "Bill Jobs"
MsgBox (Left(name, 4)) 'returns Bill
MsgBox (Left(name, 7)) 'returns Bill Jo
MsgBox (Left(name, 9)) 'returns Bill Jobs

 

 

What is the syntax to return the index of a string found within another string?

InStr (start integer, string1, string2, compare)
‘start integer – specifies starting position for search; optional parameter
‘string1 – string to be searched; mandatory parameter
‘string2 – indicate string value; mandatory parameter
‘compare - sets string comparison between 0 for binary comparison (default) or 1 for text comparison; optional
'example
Dim name As String
name = "Steve Gates"
MsgBox (InStr(1, name, "S")) 'returns 1
MsgBox (InStr(7, name, "hello")) 'returns 0
MsgBox (InStr(1, name, "Gates")) 'returns 7
MsgBox (InStr(name, "Steve")) 'returns 1

Be aware: spaces count as a character, a string’s index starts at 1, and a 0 yield means the string is not contained. The search occurs from left->right. For a right->left search, use InStrRev.

What is the syntax to format a date?

FormatDateTime(date, format)
‘date is a mandatory parameter
‘format is values 0-4 each representing a certain format; optional parameter

Possible values for format parameter:

  • 0 = vbGeneralDate – Default
  • 1 = vbLongDate – Returns date
  • 2 = vbShortDate – Returns date
  • 3 = vbLongTime – Returns time
  • 4 = vbShortTime – Returns time

‘examples
MsgBox (FormatDateTime("2064-08-15 20:25")) ‘returns 8/15/2064 8:25:00PM
MsgBox (FormatDateTime(#5/21/2075#, 1)) ‘Tuesday, May 21, 2075
MsgBox (FormatDateTime(#5/21/2075#, 2)) ‘5/21/2075
MsgBox (FormatDateTime("12:34:17", 3)) ‘12:34:17 PM
MsgBox (FormatDateTime("2050-08-21 12:34:17", 4)) ‘returns 12:34