I have a string. I want to generate a random string from this string, by replacing a number by a random number. lower character by lower character and upper character by upper character. And remaining characters as it is.
I have written the below code. I need to call this method millions of time on different strings (string length is not more than 100 characters), It's taking too much time.
private static string GenerateRandomAlphanumericValue(string input) {
char[] newStr = new char[input.Length];
char[] alphaU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
char[] alphaL = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
char[] number = "0123456789".ToCharArray();
Random random = new Random();
for (int i = 0; i < input.Length; i++) {
if (char.IsNumber(input[i])) {
int index = random.Next(0, number.Length);
newStr[i] = number[index];
}
else if (char.IsUpper(input[i])) {
int index = random.Next(0, alphaU.Length);
newStr[i] = alphaU[index];
}
else if (char.IsLower(input[i])) {
int index = random.Next(0, alphaL.Length);
newStr[i] = alphaL[index];
}
else {
newStr[i] = input[i];
}
}
return string.Join("", newStr);
}
I need help in optimizing the code or there any be a different approach to solve the problem.
Input: vivEK123$% ~a
Output: ajrLW854$% ~w