Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB8.0 VB9.0 VB VB10.0 VB.NET
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 5:11am
Edited 22-Feb-11 6:30am
v2
Comments
AlbinAbel at 22-Feb-11 10:46am
   
random text means random chars or meaningful words?
drummerboy0511 at 22-Feb-11 11:26am
   
Completely random text. It can't make any sense whatsoever.
Nishant Sivakumar at 22-Feb-11 11:42am
   
That's what my answer gives you.
Rate this: bad
good
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)
  Permalink  
Comments
drummerboy0511 at 22-Feb-11 11:28am
   
Thanks for the response... I'll try it out later and see how I like it.
Espen Harlinn at 24-Feb-11 3:50am
   
Nice and simple :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Here's a quickly put together example:
 
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 :
 
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:
 
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
  Permalink  
v3
Comments
SAKryukov at 22-Feb-11 11:59am
   
5 (what are they thinking?!)
--SA
drummerboy0511 at 22-Feb-11 18:12pm
   
This is in C#... I need it in Visual Basic...
Nishant Sivakumar at 22-Feb-11 18:14pm
   
I have updated my answer with VB code.
drummerboy0511 at 22-Feb-11 18:16pm
   
It gave me an error with the GetRandomChar() part...
Nishant Sivakumar at 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 at 22-Feb-11 18:20pm
   
I updated my answer with an alternate VB version. Please try that too.
drummerboy0511 at 22-Feb-11 19:39pm
   
Thank you! I was able to modify the second VB version to work with what I needed.
Nishant Sivakumar at 22-Feb-11 20:01pm
   
You are welcome.
Espen Harlinn at 24-Feb-11 3:50am
   
Good effort :)
Nishant Sivakumar at 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
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 210
3 Afzaal Ahmad Zeeshan 204
4 CPallini 185
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 22 Feb 2011
Copyright © CodeProject, 1999-2014
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