A string is one or a combination
of characters. To support strings, the Visual Basic language provides the String
data types. The .NET Framework provides a class named String that
is defined in the System namespace. That class provides tremendous
additional support for strings using properties and methods.

Creating a String

To declare a variable for a string, you can use either the String
data type of the Visual Basic language or the Object class of the .NET Framework.
To initialize a string variable, put its value in double-quotes and assign it to the variable. Here is an example:

<%
Dim FirstName As Object
FirstName = "William"
%>

If the string is long, you can include it on many lines.
Here are examples:

<%@ Page Language="VB" %>
<!DOCTYPE html>
<script runat="server">
Sub ddlSelectionSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Select Case ddlSelection.SelectedItem.Text
Case "Introduction"
txtDescription.Text = "Social sciences cover a wide range of studies that address
all types of topics about human beings, their behaviors, their history, their societies, and their
environments. Some social science topics are grouped in categories, such as individual or crowd,
where studies tend to address the same categorical isssues."
Case "Linguistics"
txtDescription.Text = "Linguistics is the study of one or a family of languages.
Linguistics studies the science, the technology, the mechanical, and physical aspects that rule
a language. Linguistics scientifically describes the sound produced when a particular language
is spoken, how, and usually why a certain sound is produced. Linguistics does not present or
boast the beauties of a language, but linguistics can present some lacking features or
describes some strengths. These aspects would lead to bias, deceiving the purpose of this science."
Case "Philosophy"
txtDescription.Text = "Philosophy is the study of existense (or being),
knowledge (or reasoning), and truth. Philosophy seeks to comprehend what
truth is, how to evaluate it, and how it influences thoughts and ethics.
Like other social science topics such as sociology or psychology, philosophy
examines both personal and crowd behavior but only as they relate to the
mind. An example is the thinking process that results in someone taking one
action rather than another (another person taking the same action or another
person taking a different action). Unlike the other social science fields,
philosophy doesn't concentrate on what is good or bad, or what is practical
or weird, or on how something should (is supposed to) be. Instead,
philosophy delves into the logic and the ethical reasons of what it (such
as something or a behavior) is."
Case "Psychology"
txtDescription.Text = "Psychology is a social science field that focuses on
the mind as it relates to human thoughts and behaviors. On one hand,
psychology gets inputs from sociology, philosophy, medicine, and ethnicity,
etc. on the other hand, psychology has a great influence on all sciences
that deal with personal views and actions.
Psychology does Not exclusively targets the
individual but it also considers any aspect in the person's environment.
As a matter of facts, there are various fields of studies that derive from
psychology."
End Select
End Sub
</script>
<html>
<head runat="server">
<title>Social Science Studies</title>
</head>
<body>
<h3>Social Science Studies</h3>
<form id="frmSocialScience" runat="server">
<div>
<table>
<tr>
<td>Area of Study:</td>
<td><asp:DropDownList id="ddlSelection"
AutoPostBack="true"
OnSelectedIndexChanged="ddlSelectionSelectedIndexChanged"
runat="server">
<asp:ListItem>Introduction</asp:ListItem>
<asp:ListItem>Linguistics</asp:ListItem>
<asp:ListItem>Philosophy</asp:ListItem>
<asp:ListItem>Psychology</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td style="vertical-align: top">Description:</td>
<td><asp:TextBox id="txtDescription" TextMode="MultiLine"
Columns="40" Rows="5" runat="server">
</asp:TextBox></td>
</tr>
</table>
</div>
</form>
</body>
</html>

To include a double-quote in a string, double the
double-quote. Here are examples:

Besides the & operator, to formally support string
concatenation, the String class is equipped with a method named Concat. One of the versions of this method takes two
String arguments. Its syntax is:

Public Shared Function Concat(str0 As String,
str1 As String) As String

This version takes two strings that should be concatenated.
The method returns a new string as the first added to the second. If you need to concatenate three strings, another version of this method uses the following syntax:

Public Shared Function Concat(str0 As String,
str1 As String,
str2 As String) As String

If you need to concatenate four strings, another version of this method uses the following syntax::

Public Shared Function Concat(str0 As String,
str1 As String,
str2 As String,
str3 As String) As String

Compound Concatenation

To add a character or a string to an existing string, use the
&= operator. When the operator has been used, the string is
made of the characters it previously had plus the new character(s). Here is an example:

Formatting a string consists of inserting some characteris in it to control how it should be
presented as an object. To support this operation, the String class is equipped
with a shared method named Format. The String.Format() method is overloaded in various versions.
One of its versions uses the following syntax:

Public Shared Function Format(format As String,
arg0 As Object) As String

The first argument can contain a string and a format as {0}. This is called a placeholder for a string or a number. The second
argument is the value that would be used in place of the {0} placeholder of the first argument. If you need two placeholders for values, use the following version of the method:

Public Shared Function Format(format As String,
arg0 As Object,
arg1 As Object) As String

The first argument can contain a string plus {0} and {1} anywhere in the string (but {0} must come before {1}). The second argument will be used in place of {0} in the first argument, and the third argument will be used in place the {1} placeholders.

If you need three placeholders for values, use the following version of the method:

Public Shared Function Format(format As String,
arg0 As Object,
arg1 As Object,
arg2 As Object) As String

The first argument can contain a string plus {0}, {1}, and {2} anywhere in the string (but the {0}, {1}, and {2} must appear in that order). The second argument will be used in place of {0} in the first argument, the third argument will be used in place the {1} placeholder, and the fourth argument will be used in place of {2}. Here is an example:

If you need more than three placeholders for values, use the following version of the method:

Public Shared Function Format(format As String,
ParamArray args As Object()) As String

The first argument can contain one or a
combination of {number} placeholders. The second
argument is one or a combination of values that would be orderly added to the {number}
placeholders of the first argument.

Numeric Formatting

Introduction

Number formatting consists of specifying how a number should display on a form or a webpage. To support it, the Visual
Basic language provides a function named Format. This function can be
used for different types of values The most basic technique consists of
passing it an expression that holds the value to display. The syntax of this
function is:

The simplest way to use this function is to pass it a number or a
string as argument. The function would then produce that number. Besides the Format() function, the Visual Basic
language provides some additional functions we will review below.

To control how the number
should display, you can pass the second argument of the Format()
function. This argument would be passed as a string.

A Number in a General Format

A number is in general format if it doesn't use the character known as the thousand separator, which, in US English, is the comma. To display a number in a general format, pass "G" or "g" as the second argument of the Format() function. Here is an example:

To display a number in a fixed format, pass the second argument as "F" or "f". A number is in a fixed format if:

It uses the character considered as the decimal separator, which, in US English, is the period

On the left side of the decimal separator, there will be at least one digit. For example, if you provide a number such as .5, the function would use 0 on the left of the period

The number will display with 2 digits on the right side of the decimal separator. If the argument is provided as a natural number, the function would add .00 to the right side of the number. Here is an example:

If the value is provided with one digit on the right side the decimal separator, the function would add 0 as the last digit. If the value is passed with more than one digit, the function would reduce it to two digits using the same mechanism as the Floor() or the Ceiling() function.Here is an example:

A Number in a Standard Format

The standard format is a combination of the general and the fixed formats: The number must use the decimal separator and the thousands separator, including the mechanisms to reconcile the digits on both sides of the decimal separator. To display a number in the standard format, you have various options. You can pass the second argument of the Format() function as "n" or as "N". Here are examples:

As an alternative, to let you control how a monetary value should be displayed, the Visual Basic language provides a function named FormatCurrency. Its syntax is:

Public Shared Function FormatCurrency(Expression As Object,
NumDigitsAfterDecimal As Integer,
IncludeLeadingDigit As TriState,
UseParensForNegativeNumbers As TriState,
GroupDigits As TriState) As String

To let you specify how a percentage value should appear, the Visual Basic language provides the FormatPercent() function. Its syntax is:

Public Shared Function FormatPercent(Expression As Object,
NumDigitsAfterDecimal As Integer,
IncludeLeadingDigit As TriState,
UseParensForNegativeNumbers As TriState,
GroupDigits As TriState) As String

Other Techniques of Formatting a Number

To let you further control how a numhber should display, the second argument of the Format() function can include some special characters. To represent the integral part of a number, use the # symbol. You can enter as many # signs as you want. To specify the number of digits to display on the right side of the
decimal separator, type a period on the right side of # followed by the
number of 0s representing each decimal place. Here is an example:

As it is available to other objects, an alternative is to assign Nothing to a String variable.

A Null or Empty String

A string is referred to as null if it has lost its
characters. For example, at one time a string may contain characters but those
characters get deleted. If the string is/was stored in a variable, the variable
may contain garbage.

To let you find out whether a string is empty or null,
the String class is equipped with a shared method named IsNullOrEmpty.
Its syntax is:

As mentioned already, a string is a combination of
symbols or characters. Each character in a string has an indexed position
from 1 to its last character. To let you find out what character occupies a certain position
inside of a string, the Visual Basic language provides a function named GetChar. Its syntax
is:

Public Shared Function GetChar(ByVal str As String,
ByVal Index As Integer) As Char

The first argument is the string that will be considered. If
you pass this argument as an empty string or Nothing, you would receive an
error. The second argument is the position to be considered
inside the string. The value must be between 1 and the length of the string.
Here is an example:

To let you access a character based on its position inside a
string, the String class is equipped with a member named Chars. To
access a character, add some parentheses to the Chars member and pass
the position. This time, the positions starts at 0, continues with 1, and so on. Here is an example:

To let you have a repeating character in a string, ythe
Visual Basic language provides a function named StrDup. This function is provided in two versions whose
syntaxes are:

Public Shared Function StrDup(ByVal Number As Integer,
ByVal Character As { Char | String }) As String
- or -
Public Shared Function StrDup(ByVal Number As Integer,
ByVal Character As Object) As Object

The second argument is the character that will be
duplicated. The first argument specifies the number of times to duplicate it.
Here is an example:

If you pass a number lower than 0 or higher than 255
to the Chr() function, you would receive an error. The reason you
may pass a number higher than 255 is that you may want to get a character
beyond those of US English, such as . To support such numbers, the
Visual Basic language provides another version of the function. Its syntax
is:

Public Function ChrW(ByVal CharCode As Integer) As Char

The W here represents Wide Characters. This makes it
possible to store the character in the memory equivalent to the Short
integer data type, which can hold numbers from -32768 to 32767. Normally,
you should consider that the character should fit in a Char data type,,
which should be a positive number between 0 and 65535.

To convert a character, a string or an expression to uppercase,
the Visual Basic language provides a function named UCase that also is
overloaded with two versions. These functions take one
argument as the string or expression to be considered. The syntaxes are:

Public Shared Function UCase(ByVal Value As Char) As Char
Public Shared Function UCase(ByVal Value As String) As String

Besides the UCase() and the LCase()
functions, to convert the cases of characters in a string, the Visual Basic
language provides the StrConv() function. Its syntax is:

The first argument of this
function is the string whose characters would be converted. The second argument
specifies the type of conversion to perform. This argument is a member
of the VbStrConv enumeration. From what we have learned so far, this argument
can have one of the following values:

VbStrConv.None: There will be no conversion

VbStrConv.UpperCase: This would have the same effect as the UCase$
function. It converts the first argument's characters to uppercase

VbStrConv.LowerCase: This would have the same effect as the LCase$
function. It converts the first argument's characters to lowercase

VbStrConv.ProperCase: The first character of each word of the first
argument would be converted to uppercase

The last argument is optional. It allows you to specify the
language whose rules would be used to control the conversion.

Converting a String to Uppercase or Lowercase

To let you convert a string from lowercase to uppercase, the String class provides the ToUpper()
method. It is overloaded with two versions. One of the
versions of this method uses the following syntax:

Public Function ToUpper As String

To convert a string to lowercase, you can call the String.ToLower()
method. Its syntax is:

Public Function ToLower As String

Replacing a Character or a Sub-String in a String

To let you replacing a character or a sub-string in a string, the Visual Basic language provides the Replace()
function. Its syntax is:

The first argument is the string on which the operation will
be performed. The second argument is the character or string to look for in the Expression.
If that character or string is found, the third argument is the character or
string to replace it with. Here is an example:

As an alternative, the String class is equipped with the
Replace() method that is overloaded with two versions. The version that applies to characters uses the following syntax:

Public Function Replace(oldChar As Char,
newChar As Char) As String

If you want to remove or replace a sub-string, use the following syntax:

Public Function Replace(oldValue As String,
newValue As String) As String

Copying a String

After declaring and initializing one string variable, you can
assign it to another string variable using the assignment operator. Assigning one variable to another is referred to as copying it. To formally support this operator, the String class is equipped with the Copy() method. Its syntax is:

Public Shared Function Copy(str As String) As String

Copying To a String

The String.Copy() method is used to copy all characters of one string
into another string. To let you copy only a few characters, the String class is equipped with a method named CopyTo. Its syntax is:

Public Sub CopyTo(sourceIndex As Integer,
destination As Char(),
destinationIndex As Integer,
count As Integer)

Strings Comparisons

Introduction

String comparison consists of examining the characters of
two strings with a character of one string compared to a character of the other
string with both characters at the same positions. To support this operation, the Visual Basic language provides a function named StrCmp. Its syntax is:

To support string comparison,
the String class is equipped with the Compare() overloaded method. A version
of this method uses the following syntax:

Public Shared Function Compare(strA As String,
strB As String) As Integer

When using this version of the String.Compare()
method, the case (upper or lower) of each character is considered. If you don't
want to consider this option, the String class proposes another version of the
method. Its syntax is:

Public Shared Function Compare(strA As String,
strB As String,
ignoreCase As Boolean) As Integer

The third argument allows you to ignore the case of the
characters when performing the comparison.

String Equality

To let you compare two strings for equality, the String class is equipped with the overloaded Equals() method. When calling it, pass a string variable that calls it. The
variable that calls the method is compared to the value passed as argument. If
both values are the exact same, the method returns true. The comparison is
performed considering the case of each character. Here is an example:

A sub-string is a character or a group of characters or
symbols that are part of an existing string. As one way to support strings, the
String class is equipped with a method named Substring that comes
with two versions. One of them uses the following syntax:

Public Function Substring(ByVal startIndex As Integer) As String

The argument specifies the index from where to
start building the sub-string up to the end of the string. The other version of
the method uses the following syntax:

Public Function Substring(ByVal startIndex As Integer,
ByVal length As Integer) As String

The first argument is the same as that of the first version
of the method. The second argument specifies the number of characters to
consider.

Looking for a Character or a Sub-String

To assist you with looking for a character or a sub-string
within a string, the String class provides a method named Contains. Its
syntax is:

Public Function Contains(value As String) As Boolean

The Left Sub-String of a String

To derive a string from the left characters of an existing
string, you can use a function named Left. Its syntax is:

Public Shared Function Left(ByVal str As String,
ByVal Length As Integer) As String

The first argument is the existing string. The second argument is the number of
characters counted from the left side of the string. Here is an example:

You may want to create a string using some characters either
from the left, from the right, or from somewhere inside an existing string. To
assist you with this, the Visual Basic language provides a function named Mid.
Its syntax is:

To help you find out whether a string contains a certain character or a
certain contiguous group of characters, the Visual Basic language provides the InStr() function. It is overloaded
with two versions whose syntaxes are:

In the first version of the function, the String1
argument is the string on which the operation will be performed. The String2
argument is the character or the sub-string to look for. If String2 is
found in String1 (as part of String1), the function return the
position of the first character. Here is an example: