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.
Using Path.GetRandomFileName
is one of the simpler ways to generate a random string
. This can be sufficient for many purposes.
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
.
string
Replace
function to eliminate the period from the random string
.Random
class
, we could generate letters and numbers and add them to a Char
array.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
Sometimes we need random strings, and the exact characteristics of those strings are not important. A simple method, like GetRandomString
, suffices.
String
keys for a Dictionary
for testing performance.For serious programs, a custom method might be needed. For testing, though, the Path
class
offers an included random-string
generator: GetRandomFileName
.