Click here to Skip to main content
15,878,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 22-Feb-11 5:30am
v2
Comments
Albin Abel 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.
Nish Nishant 22-Feb-11 11:42am    
That's what my answer gives you.

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.
VB
Dim garbage As String = "alksd;fsm;gkdr'ot;fkawehotjDl;lsdEkdfksdf"

and then use the SubString() method to retrieve parts of it.
VB
myOriginalString = garbage.SubString(0, myOriginalString.Length)
 
Share this answer
 
Comments
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 :)
Here's a quickly put together example:

C#
static void Main()
{
    Console.WriteLine(GetString(20));
}

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++)
    {
        sb.Append(GetRandomChar());
    }

    return sb.ToString();
}


[Edit]
~~~~~~~~

Auto-converted code in VB :

VB
Private Shared Sub Main()
    Console.WriteLine(GetString(20))
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
        sb.Append(GetRandomChar())
    Next

    Return sb.ToString()
End Function


[Edit 2]
~~~~~~~~~~

Another attempt at auto-conversion using Reflector:

VB
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
            sb.Append(Program.GetRandomChar)
        Next i
        Return sb.ToString
    End Function

    Private Shared Sub Main()
        Console.WriteLine(Program.GetString(20))
    End Sub


    ' Fields
    Private Shared rand As Random = New Random
End Class
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 22-Feb-11 11:59am    
5 (what are they thinking?!)
--SA
drummerboy0511 22-Feb-11 18:12pm    
This is in C#... I need it in Visual Basic...
Nish Nishant 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...
Nish Nishant 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900