Strings & Characters


VBAC#VB.Net
vbCrLf\r\n
System.Environment.NewLine
Microsoft.VisualBasic.Constants.vbNewline
Microsoft.VisualBasic.ControlChars.Newline
Microsoft.VisualBasic.Constants.vbCrLf
Microsoft.VisualBasic.ControlChars.CrLf
vbCr\rMicrosoft.VisualBasic.Constants.vbCr
Microsoft.VisualBasic.ControlChars.Cr
vbLf\nMicrosoft.VisualBasic.Constants.vbLf
Microsoft.VisualBasic.ControlChars.Lf
vbTab\tMicrosoft.VisualBasic.Constants.vbTab
Microsoft.VisualBasic.ControlChars.Tab

VBA.Asc

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

'VBA Code
Dim sngValue As Single
sngValue = VBA.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 = VBA.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
VBA.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 VBA.Instr(sVariable, "find this") > 0 Then

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

'C# Equivalent
if sVariable.IndexOf("find this",0) > -1 { }
'VBA Code
If VBA.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
VBA.InstrRev(sVariable, "find this", Len(sVariable) )

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

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

VBA.LCase

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

'VBA Code
VBA.LCase(sVariable)
VBA.LCase("LOWERCASE")

'C# Equivalent
sVariable.ToLower
String.Copy("LOWERCASE").ToLower()

VBA.Left

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

'VBA Code = C# Equivalent
VBA.Left(sVariable,4) = sVariable.Substring(0,4)
'VBA Code - First 4 characters
VBA.Left(sVariable,4)

'C# Equivalent
sVariable.Substring(0,4)
'VBA Code - Remove the last character
Dim sText As String = "some text"
VBA.Left(sText, Len(sText) - 1)

'C# Equivalent
sText.Substring(0,sText.Length - 1) = "some tex"

VBA.Len

LEN - Returns the number of characters in a string.

'VBA Code
VBA.Len(sVariable)

'C# Equivalent
sVariable.Length
'VBA Code
VBA.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 = C# Equivalent
VBA.Mid(sVariable,Len(sVariable),1) = sVariable.Substring(sVariable.Length - 1,1)
VBA.Mid(sVariable,Len(sVariable),1) = sVariable.Substring(sVariable.Length - 1)
VBA.Mid(sVariable,Len(sVariable)) = sVariable.Substring(sVariable.Length - 1)

VBA.Mid(sVariable,Len(sVariable),1) = sVariable.Substring(sVariable.Length - 1,1)
VBA.Mid(sVariable,Len(sVariable) - 1,1) = sVariable.Substring(sVariable.Length - 2,1)
VBA.Mid(sVariable,Len(sVariable) - 2,1) = sVariable.Substring(sVariable.Length - 3,1)
'VBA Code - return the first character
VBA.Mid(sVariable,1,1)

'C# Equivalent
sVariable.Substring(0,1)
'VBA Code - return the second character
VBA.Mid(sVariable,2,1)

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

VBA.Right

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

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

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

VBA.Right(sVariable,Len(sVariable) - 1) = sVariable.Substring(sVariable.Length - 2)
VBA.Right(sVariable,Len(sVariable) - 5) = sVariable.Substring(sVariable.Length - 6)
'VBA Code - Check the last 4 characters
If (VBA.Right(sVariable,4) = "Text") Then

'C# Equivalent
if (sVariable.EndsWith("Text") = True)
'VBA Code - Return the last character
Dim sText As String = "some text"
VBA.Right(sText,1)

'C# Equivalent
sText.Substring(sText.Length - 1) = "t"

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 = VBA.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"
VBA.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.

'VBA Code
VBA.UCase(sVariable)

'C# Equivalent
sVariable.ToUpper();

VBA.Val

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

'VBA Code
VBA.Val(sVariable)

'C# Equivalent
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
@""""""""
"'\"" 

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