VB.NET Strings

Array Collections File Keyword String Cast Class Console Data Directive Enum Exception If Interface Lambda LINQ Loop Nothing Number Process Property Regex Select Sort StringBuilder Structure Sub Time Windows

String

Strings are collections of characters.
In a String,
we find representations of words,
of phrases,
any kind of data. String-based functions are available in VB.NET. Many are well-designed.


Split

Split and Join. Suppose a string contains many parts. And you want it to have just one.
You must split it,
but if you change your mind,
you can join it back.

SplitJoin
Examples

Core. These core functions are some of the most useful and widely used. Having an understanding of them is important. This knowledge carries over to other .NET languages.

CompareConcatEqualsFormatInsertLengthRemoveReplaceString ConstructorSubstringToString
Array

String arrays. We manipulate Strings and arrays of Strings in various ways. Strings are often stored in arrays. Both types must be used together.

String ArrayReverseString LoopMid
Lowercase and uppercase words

Lower, upper. The .NET Framework provides us with many different ways to change the cases of characters in Strings.
We can use these functions
or design our own,
custom functions.

ToLowerToUpper
One: 1

First letter. We develop an optimized algorithm for uppercasing the first letter in a String. We can alternatively use ToTitleCase.

Uppercase FirstToTitleCase
Index

Search. If you need to search your String for a character pattern or a single character, these IndexOf and LastIndexOf functions are ideal. The result of IndexOf may be negative.

ContainsIndexOfIndexOfAnyLastIndexOfLastIndexOfAny
Padding: right and left

Whitespace. Some functions typically are used to handle whitespace characters.
These include Trim,
TrimStart
and TrimEnd. These functions also handle non-whitespace characters.

PadLeft, PadRightTrimTrimEndTrimStartIsNullOrEmpty, IsNullOrWhiteSpaceNewLineLSet, RSetLTrim, RTrim
String face

Empty. An empty string is not Nothing (or null). Rather it is just a string literal with a length of 0. We can use String.Empty to access an empty string.

String.Empty
Programming tip

Ends, starts. Sometimes we need to test String ends and starts. This logic can be written from scratch.
But to our relief,
we can use the EndsWith
and StartsWith functions.

EndsWithStartsWith
Convert or change

Convert. It is often necessary to convert Strings to other types, such as Integers. Conversely, most types can be converted to Strings with ToString. We also convert Lists and arrays.

CStrChar Array to StringList to StringString Array to StringString to Byte ArrayString to Integer

ToCharArray. This Function converts an immutable String into a mutable char array. This enables many optimizations and new capabilities.

ToCharArray
Select method call

Select Case. A String instance can be used in a Select Case statement. A logical decision is made based on the String's character data. In other languages, the switch-statement is similar.

Select Case, String
Steps

Distance. How many edits does it take to get from one String to another? With the Levenshtein distance algorithm, we compute this number. This tells us String similarity.

Levenshtein
ROT13 algorithm

ROT13. The ROT13 encoding algorithm shifts the letters in the alphabet by 13 places in two directions based on their values. This is a helpful learning exercise.

ROT13
Semicolon

Punctuation. We can remove punctuation with a custom method. This handles all punctuation known to the built in Char.IsPunctuation method.

Remove Punctuation
About part

Random. With a helpful method from the Path class, we can easily generate random strings. These have limitations, but work for many simple requirements.

Random String
StringBuilder

The String type is powerful. But it has limitations. In situations where we can improve performance by mutating an existing string, consider an alternative type like StringBuilder.

StringBuilder
VB.NET