How to Generate formatted Alpha Numeric strings in C# - c#

I want to generate random string with 5 alpha numeric characters, But should generate two characters and other should be numeric like
RL589
For that I have done like
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var stringChars = new char[5];
var random = new Random();
for (int i = 0; i < stringChars.Length; i++)
{
stringChars[i] = chars[random.Next(chars.Length)];
}
var finalString = new String(stringChars);
But i get confused how to arrange first two letters be characters and next other should be numeric. Please help me anyone.

Generate two strings. One of characters and one of numbers. Then, concatenate them.

Use two loops, for example like this:
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var numbers = "0123456789";
var stringChars = new char[5];
var random = new Random();
for (int i = 0; i < 2; i++)
{
stringChars[i] = chars[random.Next(chars.Length)];
}
for (int i = 2; i < stringChars.Length; i++)
{
stringChars[i] = numbers[random.Next(numbers.Length)];
}
var finalString = new String(stringChars);

var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var numbers = "0123456789";
var stringChars = new char[5];
var random = new Random();
for (int i = 0; i < stringChars.Length; i++)
{
if (i < 2)
{
stringChars[i] = letters[random.Next(letters.Length)];
}
else
{
stringChars[i] = numbers[random.Next(numbers.Length)];
}
}
var finalString = new String(stringChars);

You don't need any strings. Just use the implicit conversions between char and int:
var stringChars = new char[5];
var random = new Random();
for (int i = 0; i < 2; i++)
{
stringChars[i] = (char)random.Next('A', 'Z');
}
for (int i = 2; i < 5; i++)
{
stringChars[i] = (char)random.Next('0', '9');
}
var finalString = new String(stringChars);

Why don't you just use the Random object to create a random number?
var stringChars = new StringBuilder();
var random = new Random();
for (int i = 0; i < 2; i++)
{
stringChars.Append((char)random.Next('A', 'Z'));
}
var finalString = string.Format("{0}{1}{2}{3}", stringChars.ToString()
, random.Next(1, 9)
, random.Next(1, 9)
, random.Next(1, 9));
or simply
var finalString = string.Format("{0}{1}", stringChars.ToString()
, random.Next(100, 999));
Why StringBuilder, string concatenation always creates a new instance so you should avoid that.

Related

C# Remove An Empty Character Array

I have character arrays seperated into multiple groups, my code uses a character from every group, so once character in a group is processed it will go to the next. I want to remove a group when it's considered empty. (or alternative if you can help fix the code so it skips empty groups)
In the form, there are checkboxes which add the specific characters to their specified group, you have the option to add those characters of choice or not, which I also have a user include input textbox if they want to add their personal characters, but in a case of a char group being empty I get an exception most likely due to the group having no characters.
Exception:
"System.IndexOutOfRangeException: 'Index was outside the bounds of the
array."
For
"result[i] = charGroups[nextGroupIdx][nextCharIdx];"
char[][] charGroups = new char[][]
{
CapitalCharacterSet.ToCharArray(),
LowercaseCharacterSet.ToCharArray(),
NumbersCharacterSet.ToCharArray(),
IncludeCharacterSet.ToCharArray(),
SpecialCharacterSet.ToCharArray()
};
FULL CODE:
char[][] charGroups = new char[][]
{
CapitalCharacterSet.ToCharArray(),
LowercaseCharacterSet.ToCharArray(),
NumbersCharacterSet.ToCharArray(),
IncludeCharacterSet.ToCharArray(),
SpecialCharacterSet.ToCharArray()
};
int[] charsLeftInGroup = new int[charGroups.Length];
for (int i = 0; i < charsLeftInGroup.Length; i++)
charsLeftInGroup[i] = charGroups[i].Length;
int[] leftGroupsOrder = new int[charGroups.Length];
for (int i = 0; i < leftGroupsOrder.Length; i++)
leftGroupsOrder[i] = i;
byte[] randomBytes = new byte[4];
RNGCryptoServiceProvider rng = new();
rng.GetBytes(randomBytes);
int seed = BitConverter.ToInt32(randomBytes, 0);
Random random = new(seed);
char[] result = null;
result = new char[length.Value];
int nextCharIdx;
int nextGroupIdx;
int nextLeftGroupsOrderIdx;
int lastCharIdx;
int lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;
for (int i = 0; i < result.Length; i++)
{
if (lastLeftGroupsOrderIdx == 0)
nextLeftGroupsOrderIdx = 0;
else
nextLeftGroupsOrderIdx = random.Next(0, lastLeftGroupsOrderIdx);
nextGroupIdx = leftGroupsOrder[nextLeftGroupsOrderIdx];
lastCharIdx = charsLeftInGroup[nextGroupIdx] - 1;
if (lastCharIdx == 0)
nextCharIdx = 0;
else
nextCharIdx = random.Next(0, lastCharIdx + 1);
try
{
result[i] = charGroups[nextGroupIdx][nextCharIdx];
}
catch
{
if (lastCharIdx == 0)
nextCharIdx = 0;
else
nextCharIdx = random.Next(0, lastCharIdx + 1);
}
if (lastCharIdx == 0)
charsLeftInGroup[nextGroupIdx] = charGroups[nextGroupIdx].Length;
else
{
if (lastCharIdx != nextCharIdx)
{
char temp = charGroups[nextGroupIdx][lastCharIdx];
charGroups[nextGroupIdx][lastCharIdx] =
charGroups[nextGroupIdx][nextCharIdx];
charGroups[nextGroupIdx][nextCharIdx] = temp;
}
charsLeftInGroup[nextGroupIdx]--;
}
if (lastLeftGroupsOrderIdx == 0)
lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;
else
if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx)
{
int temp = leftGroupsOrder[lastLeftGroupsOrderIdx];
leftGroupsOrder[lastLeftGroupsOrderIdx] =
leftGroupsOrder[nextLeftGroupsOrderIdx];
leftGroupsOrder[nextLeftGroupsOrderIdx] = temp;
}
lastLeftGroupsOrderIdx--;
}
}

How do I generate a random alphanumeric array that has 3 letter and 6 digits in c#?

I am trying to generate a random alphanumeric array that consist of 3 letters and 6 digits. The entire array must be random. The only way I could think of is generating 2 individual random arrays and then merging them and randomizing the merged array. Any help would be appreciated. I specifically need help on ensuring that the correct number of variable types are stored. Here is my semi-working code:
static void Main(string[] args)
{
var alphabetic = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var numeric = "0123456789";
var stringChars = new char[9];
var random = new Random();
for (int i = 0; i < 3; i++)
{
stringChars[i] = alphabetic[random.Next(alphabetic.Length)];
}
for(int i = 3; i< stringChars.Length; i++)
{
stringChars[i] = numeric[random.Next(numeric.Length)];
}
var ranChars = new char[9];
var semisorted = new String(stringChars);
for (int i=0; i< ranChars.Length; i++)
{
ranChars[i] = semisorted[random.Next(semisorted.Length)];
}
var final = new string(ranChars);
Console.WriteLine("{0}", final);
Console.ReadLine();
}
You're close. But the problem here is that you're selecting randomly from the "semi-sorted" array, while what's really necessary at that point is picking a random permutation. One way to do that is with a Fisher-Yates shuffle.
So combining that with the code you had that worked: (not tested)
for (int i = 0; i < 3; i++)
{
stringChars[i] = alphabetic[random.Next(alphabetic.Length)];
}
for(int i = 3; i< stringChars.Length; i++)
{
stringChars[i] = numeric[random.Next(numeric.Length)];
}
int n = stringChars.Length;
while (n > 1)
{
int k = random.Next(n--);
char temp = stringChars[n];
stringChars[n] = stringChars[k];
stringChars[k] = temp;
}
string result = new string(stringChars);
Harold's answer is way cleaner, but here's another approach for the whole '100 ways to do the same thing in programming' concept. [Edit: Doh, I reversed the number of digits and letters. Here's a fix:]
public static void Main(string[] args)
{
var random = new Random();
var finalString = string.Empty;
var finalArray = new string[9];
for (var i = 0; i < 3; i++)
{
var alphabet = random.Next(0, 26);
var letter = (char) ('a' + alphabet);
finalArray[i] = letter.ToString().ToUpper();
}
for (var i = 3; i < 9; i++)
{
var number = random.Next(0, 9);
finalArray[i] = number.ToString();
}
var shuffleArray = finalArray.OrderBy(x => random.Next()).ToArray();
for (var i = 0; i < finalArray.Length; i++)
{
finalString += shuffleArray[i];
}
Console.WriteLine(finalString);
Console.ReadKey();
}

(jagged array) Array of an Array: automatically create in while loop, how to initialize?

I would like to create an array of an array from a text file...
There are 20000 line with 21 strings in each line separated by ',' .
I would like to read each line and make it into an array , each line being a new array within.
So I wanted to create the jagged array by starting it like this:
string[][] SqlArray = new string[200000][21];
But it gives: ERROR MESSAGE : Invalid rank specifier: expected ',' or ]
How would I create this array or initialize it?
I will be populating the data in the array like this:
while (true)
{
string theline = readIn.ReadLine();
if (theline == null) break;
string[] workingArray = theline.Split(',');
for (int i = 0; i < workingArray.Length; i++)
{
for (int k = 0; k < 20; k++)
{
SqlArray[i][k] = workingArray[k];
}
}
}
Thank you
That type of initialization only works in Java. You must declare an array of arrays then initialize each in a loop.
string[][] SqlArray = new string[21][];
for(int index = 0; index < SqlArray.Length; index++)
{
SqlArray[index] = new string[2000000];
}
Alternatively, you can use a non-jagged array. It will probably work for what you need.
string[,] SqlArray = new string[21 , 2000000];
It can be accessed like so:
SqlArray[2,6264] = x;
To anyone who is interested this is how I ended up implementing it:
TextReader readIn = File.OpenText("..\\..\\datafile.txt");
string[][] SqlArray = new string[rowNumCreate][];
int e = 0;
while (true)
{
string theline = readIn.ReadLine();
if (theline == null) break;
string[] workingArray = theline.Split(',');
SqlArray[e] = new string[valuesInRow +1];
for (int k = 0; k < workingArray.Length; k++)
{
SqlArray[e][k] = workingArray[k];
}
e++;
}
The file being read is a simple mock database set as a flat file that was auto-generated to test an algorithm that I am implementing, which works with jagged arrays; hence instead of working with a data base I just created this for ease of use and to increase and decrease size at will.
Here is the code to build the text file:
Random skill_id;
skill_id = new Random();
// int counter =0;
string seedvalue = TicksToString();
int rowNumCreate = 200000;
int valuesInRow = 20;
string lineInFile = seedvalue;
string delimiter = ",";
for (int i = 0; i < rowNumCreate; i++)
{
for (int t = 0; t < valuesInRow; t++)
{
int skill = skill_id.Next(40);
string SCon = Convert.ToString(skill);
lineInFile += delimiter + SCon;
}
if (rowNumCreate >= i + 1)
{
dataFile.WriteLine(lineInFile);
lineInFile = "";
string userPK = TicksToString();
lineInFile += userPK;
}
}
dataFile.Close();
public static string TicksToString()
{
long ms = DateTime.Now.Second;
long ms2 = DateTime.Now.Millisecond;
Random seeds;
seeds = new Random();
int ran = seeds.GetHashCode();
return string.Format("{0:X}{1:X}{2:X}", ms, ms2, ran).ToLower();
}
I am still a student so not sure if the code is A-grade but it works :)

random uppercase in a word + multiple string insert

I really need a solution for the next scenario(I've been searching for hours and beating about the bushes to find a smooth solution, but none worked):
I have a winform that:
parse a text file
generate some folders using random words from that file
My code so far:
int value;
string path = null;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog(this) == DialogResult.OK)
{
path = fbd.SelectedPath;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
value = Convert.ToInt32(textBox1.Text);//store the value from the textbox in variable "value"
}
private void button2_Click(object sender, EventArgs e)
{
if (path != null && Directory.Exists(path))
for (int i = 0; i < value; i++)
{
Random rnd = new Random();
string tick1 = rnd.Next(0, 10).ToString();
var lines = File.ReadAllLines(#"M:\\dictionar.txt");
var r = new Random(DateTime.Now.Millisecond);
var randomLineNumber = r.Next(0, lines.Length - 1);
var line = lines[randomLineNumber];
StringBuilder b = new StringBuilder();
for (int j = 0; j < line.Length; j++)
{
char c = line[j];
if (rnd.Next(2) == 0)
{
c = Char.ToUpper(c);
}
b.Append(c);
if (j % 3 == 2)
{
b.Append(rnd.Next(10));
}
}
line = b.ToString();
Directory.CreateDirectory(Path.Combine(path, string.Format("{0}", line.Insert(2, tick1).Insert(4, tick1).Insert(6, tick1))));
}
}
Is there a way to use ToUpper() method as line.Insert() one so that I can get random upper letters? More, is there a better way of randomizing those index positions form line.Insert() (I'm asking this because when it's generating the folders name: the indexes are the same: e.g pe8rs8on8al and just after that the index changes.)?
I want to achieve the following:
if I have the next words in the .txt file:
personal
football
programming
computer
I would like the folder names to look like:
Pe3rs9oN1al
fO8ot5Ba6lL
You can loop through the characters in the string and build a new string depending on random values:
StringBuilder b = new StringBuilder();
for (int i = 0; i < line.Length; i++ ) {
char c = line[i];
if (rnd.Next(2) == 0) {
c = Char.ToUpper(c);
}
b.Append(c);
if (i % 2 == 1) {
b.Append(rnd.Next(10));
}
}
line = b.ToString();
Note: You shouldn't create Random objects in the loop. You should create a single Random object before the loop and use for all random numbers that you need. Creating instances too close in time will make them return the same sequences of random numbers. Also, you don't need to seed the random generator from the clock, the constructor without parameters does that automatically:
Random rnd = new Random();
So, the code in the method would be:
if (path != null && Directory.Exists(path))
Random rnd = new Random();
for (int i = 0; i < value; i++)
{
var lines = File.ReadAllLines(#"M:\\dictionar.txt");
var randomLineNumber = rnd.Next(0, lines.Length);
var line = lines[randomLineNumber];
StringBuilder b = new StringBuilder();
for (int j = 0; j < line.Length; j++)
{
char c = line[j];
if (rnd.Next(2) == 0)
{
c = Char.ToUpper(c);
}
b.Append(c);
if (j % 2 == 1)
{
b.Append(rnd.Next(10));
}
}
line = b.ToString();
Directory.CreateDirectory(Path.Combine(path, line));
}
}
Note the rnd.Next(0, lines.Length) to pick a random line. The upper limit for the random number is not inclusive, so if you use rnd.Next(0, lines.Length - 1) it will never pick the last line.
That's because you are specifying only tick1 in the same loop. If you want to change this, add additional ticks to your code as below:
string tick1 = rnd.Next(0, 10).ToString();
string tick2 = rnd.Next(0, 10).ToString();
string tick3 = rnd.Next(0, 10).ToString();
Then use those in your formatting of the string:
Directory.CreateDirectory(Path.Combine(path, string.Format("{0}", line.Insert(2, tick1).Insert(4, tick2).Insert(6, tick3))))
Like Guffa said you should not use Random in a loop, in all preference you should only instanciate one of it, but I think you could use it like this
public static class StringRandomize
{
static readonly Random rnd = new Random();
static char[] permmitedCharacters { get; set; }
static StringRandomize()
{
List<char> Chars= new List<char>();
for (int i = 48; i < 48+10; i++)
{
Chars.Add((char)i);
}
for (int i = 65; i < 65+26; i++)
{
Chars.Add((char)i);
}
permmitedCharacters = Chars.ToArray();
}
public static string Randomize(string input, double RandomizePercent = 30)
{
StringBuilder result = new StringBuilder();
int index = 0;
while (index < input.Length)
{
if (rnd.Next(0, 100) <= RandomizePercent)
{
if (rnd.Next(0, 100) <= RandomizePercent)
{
result.Append(GenerateCaracter());
}
else
{
if (rnd.Next(0, 100) > 50)
{
result.Append(input.ToLower()[index]);
}
else
{
result.Append(input.ToUpper()[index]);
}
index++;
}
}
else
{
result.Append(input[index]);
index++;
}
}
return result.ToString();
}
private static char GenerateCaracter()
{
return permmitedCharacters[rnd.Next(0, permmitedCharacters.Length)];
}
}
private static void GenerateRandomDirectories(string path, int value)
{
//I'm supposing value is the number of lines that you want
var lines = File.ReadAllLines(#"M:\\dictionar.txt");
Random rnd = new Random();
if (path != null && Directory.Exists(path))
{
for (int i = 0; i < value; i++)
{
Directory.CreateDirectory(path + "\\" + StringRandomize.Randomize(lines[rnd.Next(0,lines.Length)]));
}
}
}
"pers3o7Nal"
"foOtBaLl"
Got like this
public Form1()
{
InitializeComponent();
string content = "";
using (FileStream fs = new FileStream("D:\\names.txt", FileMode.Open, FileAccess.Read))
using (StreamReader sr = new StreamReader(fs))
content = sr.ReadToEnd();
string[] names = content.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
string path = "D:\\RandDirs";
if (!Directory.Exists(path))Directory.CreateDirectory(path) ;
for (int i = 0; i < 50; i++) Directory.CreateDirectory(path + "\\" + getRandomName(names));
}
Random randName = new Random();
Random insertingNumber = new Random();
Random randUpper = new Random();
Random randInsertNumber = new Random();
string getRandomName(string[] names)
{
string name = names[randName.Next(names.Length)];
name = name.Replace(" ", "");
string result = "";
for (int i = 0; i < name.Length; i++)
result += (randUpper.Next(0, 9) <= 5 ? name[i].ToString().ToLower() : name[i].ToString().ToUpper())
+ (((i + 1) % 2 == 0) ? insertingNumber.Next(0, 9).ToString() : "");
return result;
}
as per your needs, i've changed from randomly inserting numbers to inserting number every 2 characters.

Remove words from string using substring

Is there a method that will remove a set amount of characters from a string, placing the removed characters into a separate string and leaving the original x amount of characters shorter?
I need to parse a string into 10 individual strings, each 10 characters long. I would like to be able to do sommething simple like this, but I do not know if there is a method that works like this in C#
string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
errorCodes[i] = retrievedMessage.removeFromSubstring(0, 10);
}
You could try this:
string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
errorCodes[i] = retrievedMessage.Substring(0, 10);
retrievedMessage = retrievedMessage.Substring(10);
}
The line retrievedMessage = retrievedMessage.Substring(10); will effectively remove the first 10 characters from the original string. This way in each iteration you will be able to use the first 10 characters and assign them to the errorCodes[i]
Also you could try to avoid using substrings:
string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
errorCodes[i] = retrievedMessage.Substring(i*10, 10);
}
Edit
Now tested, seems to work fine for me
var errorCodes = "longstringgggggggggggggggggggggggggg";
var count = 10;
List<string> s = new List<string>();
for (int i = 0; i < errorCodes.Length; i += count)
{
if (i + count > errorCodes.Length)
count = errorCodes.Length - i;
s.Add(errorCodes.Substring(i, count));
}
foreach (var str in s)
Console.WriteLine(str);
Console.ReadLine();
This should work for you
string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
errorCodes[i] = retrievedMessage.Substring(10*i, 10);
}
Here is an option that will remove from the string retrievedMessage
string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
//option to remove from string
errorCodes[i] = retrievedMessage.Substring(0, 10);
retrievedMessage = retrievedMessage.Remove(0,10); //will remove from string
}
Same basic concept as other answers but with a little checking for variable string length. If you know that your string is always 100 chars in length, then use one of the simpler answers.
string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
int startIndex = i * 10;
if (retrievedMessage.Length > startIndex)
{
int length = 10;
if (retrievedMessage.Length < (startIndex + length))
{
length = retrievedMessage.Length - startIndex;
}
errorCodes[i] = retrievedMessage.Substring(startIndex, length);
}
}
Note: Since errorCodes is always instantiated with a length of 10, this will have null strings if the length of retrievedMessage is <= 90. If you expect variable length, better to use a List<string> than a string[].

Categories