Home
VB.NET
String LSet and RSet Functions
Updated Sep 16, 2024
Dot Net Perls
LSet, RSet. LSet and RSet are string padding Functions. They allow you to pad the right (LSet) and left (RSet) sides of a string with spaces.
Here we look at these VB.NET functions in more detail. Usually in VB.NET we prefer PadRight and PadLeft on strings—these are more common because they are used in C#.
First example. Let's begin by looking at the LSet and RSet in a program. We see that the string returned by LSet is padded to 20 characters total by adding spaces to the right.
And The string returned by RSet, meanwhile, is padded to 20 characters total by adding spaces to the left.
Module Module1 Sub Main() ' Use LSet and RSet. Console.WriteLine("[{0}]", LSet("abcd", 20)) Console.WriteLine("[{0}]", RSet("abcd", 20)) Console.WriteLine("[{0}]", LSet("abcd", 2)) Console.WriteLine("[{0}]", RSet("abcd", 2)) End Sub End Module
[abcd ] [ abcd] [ab] [ab]
Internals. The implementation of LSet and RSet in the Microsoft.VisualBasic.dll is simple. LSet internally calls PadRight. RSet internally calls PadLeft.
However LSet and RSet are more complex than just the pad methods. The implementation has a path that calls Substring.
Internals, substring. These functions call Substring if you specify that the string be reduced below its current length. So they either pad or truncate.
String Substring
Tip It is often a better idea just to use PadRight, PadLeft and Substring directly. This is more standard practice in the .NET Framework.
String PadRight, PadLeft
Summary. We looked at LSet and RSet. We peeked inside the Visual Basic implementation. These functions truncate or pad strings without any If-statements added directly to your code.
If
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 16, 2024 (edit).
Home
Changes
© 2007-2025 Sam Allen