Random String. A random string is sometimes useful. With it, we can store data with a random key. In VB.NET we can generate a random string in many ways.
Example. In .NET, an included method called Path.GetRandomFileName returns an 8-char file name with a 3-char extension. This will generate a short, lowercased, random string.
Warning There are limitations to this approach. Longer strings are not available, and no uppercase letters are included.
Detail We use the string Replace function to eliminate the period from the random string.
Imports System.IO
Module Module1
Public Function GetRandomString()
Dim p As String = Path.GetRandomFileName()
p = p.Replace(".", "")
Return p
End Function
Sub Main()
' Get two random strings.
Dim value As String = GetRandomString()
Dim value2 As String = GetRandomString()
' Display the strings.
Console.WriteLine(value)
Console.WriteLine(value2)
End Sub
End Moduleqkdqh2lmcev
tsx1vo4fdhh
A discussion. Sometimes we need random strings, and the exact characteristics of those strings are not important. A simple method, like GetRandomString, suffices.
Info For example, I have needed to generate random String keys for a Dictionary for testing performance.
A summary. For serious programs, a custom method might be needed. For testing, though, the Path class offers an included random-string generator: GetRandomFileName.
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 7, 2022 (rewrite).