Click here to Skip to main content
13,057,089 members (43,798 online)
Rate this:
Please Sign up or sign in to vote.
Okay, I want to replace a string with a string of the same length, but with random text. How would I do this? I know that I need to get the length of the string (I already know how to do that), then get random characters and replace each of the original characters with the random ones (don't know how to do that yet.)

Edit: It has to be completely random text, not just random words.
Posted 22-Feb-11 4:11am
Updated 22-Feb-11 5:30am
AlbinAbel 22-Feb-11 10:46am
random text means random chars or meaningful words?
drummerboy0511 22-Feb-11 11:26am
Completely random text. It can't make any sense whatsoever.
Nishant Sivakumar 22-Feb-11 11:42am
That's what my answer gives you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

There are several ways to do what you want, which would be easier/quicker will depend on how long the largest string you need to process is.

You could declare a string variable and instantiate it with garbage characters.
Dim garbage As String = "alksd;fsm;gkdr'ot;fkawehotjDl;lsdEkdfksdf"

and then use the SubString() method to retrieve parts of it.
myOriginalString = garbage.SubString(0, myOriginalString.Length)
drummerboy0511 22-Feb-11 11:28am
Thanks for the response... I'll try it out later and see how I like it.
Espen Harlinn 24-Feb-11 3:50am
Nice and simple :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Here's a quickly put together example:

static void Main()
static Random rand = new Random();
private static char GetRandomChar()
    return (char)rand.Next('a', 'z');
private static string GetString(int length)
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < length; i++)
    return sb.ToString();


Auto-converted code in VB :

Private Shared Sub Main()
End Sub
Shared rand As New Random()
Private Shared Function GetRandomChar() As Char
    Return CChar(rand.[Next]("a"C, "z"C))
End Function
Private Shared Function GetString(length As Integer) As String
    Dim sb As New StringBuilder()
    For i As Integer = 0 To length - 1
    Return sb.ToString()
End Function

[Edit 2]

Another attempt at auto-conversion using Reflector:

Friend Class Program
    ' Methods
    Private Shared Function GetRandomChar() As Char
        Return DirectCast(Program.rand.Next(&H61, &H7A), Char)
    End Function
    Private Shared Function GetString(ByVal length As Integer) As String
        Dim sb As New StringBuilder
        Dim i As Integer
        For i = 0 To length - 1
        Next i
        Return sb.ToString
    End Function
    Private Shared Sub Main()
    End Sub

    ' Fields
    Private Shared rand As Random = New Random
End Class
SAKryukov 22-Feb-11 11:59am
5 (what are they thinking?!)
drummerboy0511 22-Feb-11 18:12pm
This is in C#... I need it in Visual Basic...
Nishant Sivakumar 22-Feb-11 18:14pm
I have updated my answer with VB code.
drummerboy0511 22-Feb-11 18:16pm
It gave me an error with the GetRandomChar() part...
Nishant Sivakumar 22-Feb-11 18:18pm
Maybe the brackets are not needed around the call to Next. Try removing that. I don't really know VB syntax, so I used a converter that takes C# and gives VB syntax output.
Nishant Sivakumar 22-Feb-11 18:20pm
I updated my answer with an alternate VB version. Please try that too.
drummerboy0511 22-Feb-11 19:39pm
Thank you! I was able to modify the second VB version to work with what I needed.
Nishant Sivakumar 22-Feb-11 20:01pm
You are welcome.
Espen Harlinn 24-Feb-11 3:50am
Good effort :)
Nishant Sivakumar 24-Feb-11 8:31am
Thanks Espen!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170728.6 | Last Updated 22 Feb 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100