Strings & Characters

Visual Basic 2005 replaces the old VB 6.0 functions with properties

VBA.NET
vbTabMicrosoft.VisualBasic.ControlChars.Tab
vbCrMicrosoft.VisualBasic.ControlChars.Cr
vbCrLfSystem.Environment.NewLine
Microsoft.VisualBasic.ControlChars.Newline

VBA.Asc

ASC - Returns the ANSI number for the first character in a text string.

'VBA Code
Dim sngValue As Single
sngValue = Asc("A") '=65

'C# Equivalent
Single sngValue;
char tempCharacter = "A"c;
char[] getCharacters = tempCharacter;
char convertCharacter = System.Text.ASCIIEncoding.ASCII.GetBytes(getCharacters)[0];
sngValue = System.Convert.ToSingle(convertCharacter);
'VBA Code
Dim sngValue As Single
sngValue = Asc("Asometext") '=65

'C# Equivalent
string tempString = "Asometext";
Single sngValue;
char[] getCharacters = tempString.ToCharArray();
char convertCharacter = (char)System.Text.ASCIIEncoding.ASCII.GetBytes(getCharacters)[0];
sngValue = System.Convert.ToSingle(convertCharacter);

VBA.Chr

CHR - Returns the character with the corresponding ANSI number.

'VBA Code
Chr(65) = "A"

'C# Equivalent
System.Convert.ToChar(65) = "A"

VBA.InStr

INSTR - Returns the position of a substring within a larger string.
In VBA, Instr() returns 1 when the find item is at the start of the string and is also case sensitive
In VSTO, sText.IndexOf() returns 0 when the find item is at the start of the string and is also case sensitive

'VBA Code
If Instr(sVariable, "find this") > 0 Then

'C# Equivalent
if sVariable.IndexOf("find this") > -1 { }
'VBA Code
If Instr(1,sVariable,"find this") > 0 Then

'C# Equivalent
if sVariable.IndexOf("find this",0) > -1 { }
'VBA Code
If Instr(----) > 0 Then

'C# Equivalent
If (sText.IndexOf(-----) > -1) Then

For more details, please refer to the Strings & Characters > Finding


VBA.InStrRev

INSTRREV - Returns the position of a substring within a larger string, from the end.

'VBA Code
InstrRev(sVariable, "find this", Len(sVariable) )

'C# Equivalent
sVariable.Reverse.IndexOf("find this", 0) - check this !!!
'VBA Code
Do While InstrRev(---)
Loop

'C# Equivalent
while sText.IndexOf(---) > -1
{
}

VBA.LCase

LCASE - Returns the text string with all characters converted to lowercase.

'VBA Code
LCase(sVariable)

'C# Equivalent
sVariable.ToLower

VBA.Left

LEFT - Returns a number of characters from the left of a string.

'VBA Code
Left(sVariable,4)

'C# Equivalent
sVariable.Substring(0,4)
'VBA Code
Left(sVariable,1)

'C# Equivalent
sVariable.Substring(0,1)

VBA.Len

LEN - Returns the number of characters in a string.

'VBA Code
Len(sVariable)

'C# Equivalent
sVariable.Length
'VBA Code
Len("my string")

'C# Equivalent
sVariable = "my string"
sVariable.Length

VBA.Mid

MID - Returns the text string which is a substring of a larger string.

'VBA Code
Mid(sVariable,1,1)

'C# Equivalent
sVariable.Substring(0,1)
'VBA Code
Mid(sVariable,2,1)

'C# Equivalent
sVariable.Substring(1,1)

'VBA Code = C# Equivalent
Mid(sVariable,Len(sVariable),1) = sVariable.Substring(sVariable.Length - 1,1)
Mid(sVariable,Len(sVariable),1) = sVariable.Substring(sVariable.Length - 1)
Mid(sVariable,Len(sVariable)) = sVariable.Substring(sVariable.Length - 1)

Mid(sVariable,Len(sVariable),1) = sVariable.Substring(sVariable.Length - 1,1)
Mid(sVariable,Len(sVariable) - 1,1) = sVariable.Substring(sVariable.Length - 2,1)
Mid(sVariable,Len(sVariable) - 2,1) = sVariable.Substring(sVariable.Length - 3,1)

VBA.Right

RIGHT - Returns the number of characters from the right of a text string.

'VBA Code = C# Equivalent
Right(sVariable,1) = sVariable.Substring(sVariable.Length - 1)
Right(sVariable,2) = sVariable.Substring(sVariable.Length - 2)

Right(sVariable,Len(sVariable),1) = sVariable.Substring(sVariable.Length - 1,1)
Right(sVariable,Len(sVariable),1) = sVariable.Substring(sVariable.Length - 1)

Right(sVariable,Len(sVariable) - 1) = sVariable.Substring(sVariable.Length - 2)
Right(sVariable,Len(sVariable) - 5) = sVariable.Substring(sVariable.Length - 6)
If Right(sVariable,4) = "Text" Then = If sVariable.EndsWith("Text") = True Then 

Dim sText As String = "some text"
sText.Substring(sText.Length - 1) = "
sText.Substring(1,sText.Length - 1) = "ome text"

VBA.Space

SPACE - Returns the specified number of spaces.

'VBA Code
Select Case sCountry & Space(20 - Len(sCountry))
   Case "England " : Call Something
   Case "Germany " : Call Something
   Case "France " : Call Something
End Select

'C# Equivalent
switch (sCountry.PadRight(20))
{
   case "England " : Call Something
   case "Germany " : Call Something
   case "France " : Call Something
}

'VBA Code
sValue = Space(5) & "name"
'C# Equivalent
sValue = String.Empty.PadLeft(5) + "name"

VBA.Split

SPLIT - Returns the array containing a specified number of substrings.

'VBA Code
Dim sStringConCat As String
Dim sValues As Variant
sStringConcat = "one,two,three,four"
sValues = Split(sStringConCat, ",")

'C# Equivalent
string sStringConCat;
string[] sValues;
sStringConcat = "one,two,three,four";
sValues = sStringConcat.Split(",");
sValues = sStringConcat.Split(sDeimiter.ToCharArray());

VBA.Replace

REPLACE - Returns the text string with a number of characters replaced

'VBA Code
Dim sValue As String
sValue = "this is the old text"
Replace(sValue,"old text","new text") = "this is the new text"

'C# Equivalent
string sValue
sValue = "this is the old text";
sValue.Replace("old text","new text") = this is the new text"

VBA.UCase

UCASE - Returns the text string with all the characters converted to uppercase.

UCase(sVariable) 
sVariable.ToUpper();

VBA.Val

VAL - Returns the numbers contained in a string as a numeric value of the appropriate datatype.

Val(sVariable) 
System.Int32.TryParse(sVariable, out iValue);

No Fixed Length Strings

In VBA if you assign text that is longer than a fixed length string it is just truncated (?)
IN VB.NET fixed length strings are not supported.


Functions for Bytes / Double Bytes

All the string functions for handling bytes and double bytes in VBA have been removed in VB.NET
In VB.NET all strings are unicode, so converting strings to double-byte character sets is no longer necessary.


C#VB.NET
sString1 + sString2sString1 & sString2
(plus)(ampersand)
  
While (String.IsNullOrEmpty(sMyString) == false)While (Len(sMyString) > 0
 what is string is null
  
sText = "some text\r\n"sText = "some text"
  
\r\n (escape sequence)vbCrLf
\r (escape sequence)vbCr
\n (escape sequence)vbLf
 vbNewLine
 vbNullString
\t (escape sequence)vbTab
\\ (escape sequence)vbBack
 vbFormFeed
 vbVerticalTab
@""""""""
"'\"" 

© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext