Reverse a string and randomly change the position of character of a string using C# and VB.Net

Last Reply on Mar 03, 2017 07:12 AM By dharmendr

Posted on Mar 03, 2017 02:56 AM

Hi

 

How count array in normal and reverse and random ?

 

For example:

123456

654321

321456

456321

213456

214356

Means all the way count ?

Because i am trying to do my parameters in url

http://xyz.com?id=0&name=manish&hours=45

 

how to add more parameters and change parameters

 

 

 

Posted on Mar 03, 2017 02:57 AM

Hi mvadukul,

Refer the below code.

C#

int[] array = new int[] { 123456, 654321, 321456, 456321, 213456, 214356 };
// Get array count.
int count = array.Length;
// Get reverse array.
int[] arrayReverse = array.Reverse().ToArray();
// Get random no.
var random = new Random();
int randomNumber = array[random.Next(array.Length)];

VB.Net

Dim array As Integer() = New Integer() {123456, 654321, 321456, 456321, 213456, 214356}
' Get array count.
Dim count As Integer = array.Length
' Get reverse array.
Dim arrayReverse As Integer() = array.Reverse().ToArray()
' Get random no.
Dim random = New Random()
Dim randomNumber As Integer = array(random.[Next](array.Length))

 


Posted on Mar 03, 2017 05:05 AM

Hi

 

I need in String random and reverse 

 

For example random

A,B,C,D,E

IT WILL COUNT ALL 5 STRING BUT RANDOM LIKE B,E,C,A,D


Posted on Mar 03, 2017 07:12 AM

Hi mvadukul,

Refer the below code.

C#

string testString = "A,B,C,D,E";
// Get reverse string.
char[] charArray = testString.ToCharArray();
Array.Reverse(charArray);
string reverse = new string(charArray);
        
// Get random string.
Random random = new Random();
char[] randomArray = testString.Replace(",", "").ToCharArray().OrderBy(x => random.Next()).ToArray();
string finalString = string.Join(",", randomArray);

VB.Net

Dim testString As String = "A,B,C,D,E"
' Get reverse string.
Dim charArray As Char() = testString.ToCharArray()
Array.Reverse(charArray)
Dim reverse As New String(charArray)

' Get random string.
Dim random As New Random()
Dim randomArray As Char() = testString.Replace(",", "").ToCharArray().OrderBy(Function(x) random.[Next]()).ToArray()
Dim finalString As String = String.Join(",", randomArray)