How can I work with Char Array and StringBuilder? - c#

I'm trying to create random character combination. Then I'd like to output it on the console. The following code does work as long as I'm using string instead of char datatypes.
The problem with using just sting datatype is, that the output will only be in upper case and I can't simply lowercase everything and only uppercase the first string.
So I tried to do it with a char array appending every character to a stringBuilder. But somehow I can't Console.WriteLine what I coded. and I don't know why... Do you guys maybe have a hint for solving the issue?
cheers
using System;
namespace WinGen
{
public class NamenGenerator
{
private int NamenLaenge { get; set; }
private char[] BuchstabenKombination { get; set; }
private int CharShifter { get; set; }
private int CharPicker { get; set; }
private int RangeMin { get; set; }
private int RangeMax { get; set; }
public NamenGenerator()
{
Random _namenLaenge = new Random();
this.NamenLaenge = _namenLaenge.Next(4, 10);
CharShifter = 0;
RangeMin = 0;
RangeMax = 25;
BuchstabenKombination = new char[this.NamenLaenge];
}
public void StringGenerator()
{
try
{
while (this.NamenLaenge != 0)
{
Random charakter = new Random();
CharPicker = charakter.Next(RangeMin, RangeMax);
BuchstabenKombination[CharShifter] = Convert.ToChar((Buchstaben)CharPicker);
this.NamenLaenge--;
this.CharShifter++;
}
StringBuilder sb = new StringBuilder();
string t = string.Empty;
foreach (char c in BuchstabenKombination)
{
t = Convert.ToString(c);
sb.Append(t);
Console.WriteLine(t);
}
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
}
}

So you can rewrite your loop like this:
foreach (char c in BuchstabenKombination)
{
sb.Append(c);
}
Console.WriteLine(sb.ToString());
This will print the entire completed string to the console.
The other issue is that you're generating characters between 0 and 25, which are mostly control codes according to the ASCII chart. I understand that you tried to mitigate this by using an enum, but enums are essentially named integer values, so you're basically still generating characters from 0 and 25 on the ASCII chart.
To verify this, you can change your code to
BuchstabenKombination[CharShifter] = Convert.ToChar(CharPicker + 65); // aka CharPicker + 'A'
This will now generate characters A-Y.
I imagine you created an enum so that you can choose from a set of characters. Overall, I'd rewrite your code like this:
Random charakter = new Random();
StringBuilder sb = new StringBuilder();
int desiredLength = 5;
const string characters = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < desiredLength; ++i)
{
int charIdx = charakter.Next(0, characters.Length);
sb.Append(characters[charIdx]);
}
Console.WriteLine(sb.ToString()); // outputs a 5 character long string with characters randomly chosen from the characters string
Try it online

Related

How to randomly generate a number or characters in an Authorization Class file

I need help creating a class file that randomly generates a number of 10 characters, 0-9, A-Z. By generating a PIN it will create an authorization workflow to allow the user to authenticate the app. This number will be directed to a config file instead of a URL. The user goes thru a auth process and instead of calling back a URL there will be a page to randonly generate a pin. The randomly generated PIN and access code will be stored in a database table.
This is what I have so far, will this code work if not what do I need to do to make it better. I also know that a RNGCryptoServiceProvider class is good for a strong random number generator but I'm not sure how to incorporate that into my code.
I also am using get set for the database fields. Would this be the best approach for REST API? I am not very familiar with API's. I would appreciate it any help on my code.
using System;
namespace AuthorizationPIN
{
using System;
using System.Text;
class RandomNumberSample
{
static void Main(string[] args)
{
RandomGenerator generator = new RandomGenerator();
string str = generator.RandomString(10, false);
Console.WriteLine($"Random string of 10 chars is {str}");
Console.ReadKey();
}
}
public class AuthorizationPIN
{
public int Id { get; set; }
public string AccessCode { get; set; }
public string PIN { get; set; }
}
public class RandomGenerator
{
// Generate a random number between two numbers
public int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
// Generate a random string with a given size
public string RandomString(int size, bool upperCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (upperCase)
return builder.ToString().ToUpper();
return builder.ToString();
}
// Generate a random password
}
There are plenty ways to do what you ask, one way, with a good distribution could be the following:
public string RandomString(int size, bool upperCase)
{
using (var r = new RNGCryptoServiceProvider())
{
StringBuilder builder = new StringBuilder();
var data = new byte[size];
r.GetNonZeroBytes(data);
var b = #"0123456789abcdifghijklmnopqrstuvwxwzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdifghijklmnopqrstuvwxwzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdifghijklmnopqrstuvwxwzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdifghijklmnopqrstuvwxwzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456";
for (int i = 0; i < data.Length; i++)
{
var c = b[data[i]];
builder.Append((upperCase) ? char.ToUpperInvariant(c) : c);
}
return builder.ToString();
}
}

How to auto-increment number and letter to generate a string sequence wise in c#

I have to make a string which consists a string like - AAA0009, and once it reaches AAA0009, it will generate AA0010 to AAA0019 and so on.... till AAA9999 and when it will reach to AAA9999, it will give AAB0000 to AAB9999 and so on till ZZZ9999.
I want to use static class and static variables so that it can auto increment by itself on every hit.
I have tried some but not even close, so help me out thanks.
Thanks for being instructive I was trying as I Said already but anyways you already want to put negatives over there without even knowing the thing:
Code:
public class GenerateTicketNumber
{
private static int num1 = 0;
public static string ToBase36()
{
const string base36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sb = new StringBuilder(9);
do
{
sb.Insert(0, base36[(byte)(num1 % 36)]);
num1 /= 36;
} while (num1 != 0);
var paddedString = "#T" + sb.ToString().PadLeft(8, '0');
num1 = num1 + 1;
return paddedString;
}
}
above is the code. this will generate a sequence but not the way I want anyways will use it and thanks for help.
Though there's already an accepted answer, I would like to share this one.
P.S. I do not claim that this is the best approach, but in my previous work we made something similar using Azure Table Storage which is a no sql database (FYI) and it works.
1.) Create a table to store your running ticket number.
public class TicketNumber
{
public string Type { get; set; } // Maybe you want to have different types of ticket?
public string AlphaPrefix { get; set; }
public string NumericPrefix { get; set; }
public TicketNumber()
{
this.AlphaPrefix = "AAA";
this.NumericPrefix = "0001";
}
public void Increment()
{
int num = int.Parse(this.NumericPrefix);
if (num + 1 >= 9999)
{
num = 1;
int i = 2; // We are assuming that there are only 3 characters
bool isMax = this.AlphaPrefix == "ZZZ";
if (isMax)
{
this.AlphaPrefix = "AAA"; // reset
}
else
{
while (this.AlphaPrefix[i] == 'Z')
{
i--;
}
char iChar = this.AlphaPrefix[i];
StringBuilder sb = new StringBuilder(this.AlphaPrefix);
sb[i] = (char)(iChar + 1);
this.AlphaPrefix = sb.ToString();
}
}
else
{
num++;
}
this.NumericPrefix = num.ToString().PadLeft(4, '0');
}
public override string ToString()
{
return this.AlphaPrefix + this.NumericPrefix;
}
}
2.) Make sure you perform row-level locking and issue an error when it fails.
Here's an oracle syntax:
SELECT * FROM TICKETNUMBER WHERE TYPE = 'TYPE' FOR UPDATE NOWAIT;
This query locks the row and returns an error if the row is currently locked by another session.
We need this to make sure that even if you have millions of users generating a ticket number, it will not mess up the sequence.
Just make sure to save the new ticket number before you perform a COMMIT.
I forgot the MSSQL version of this but I recall using WITH (ROWLOCK) or something. Just google it.
3.) Working example:
static void Main()
{
TicketNumber ticketNumber = new TicketNumber();
ticketNumber.AlphaPrefix = "ZZZ";
ticketNumber.NumericPrefix = "9999";
for (int i = 0; i < 10; i++)
{
Console.WriteLine(ticketNumber);
ticketNumber.Increment();
}
Console.Read();
}
Output:
Looking at your code that you've provided, it seems that you're backing this with a number and just want to convert that to a more user-friendly text representation.
You could try something like this:
private static string ValueToId(int value)
{
var parts = new List<string>();
int numberPart = value % 10000;
parts.Add(numberPart.ToString("0000"));
value /= 10000;
for (int i = 0; i < 3 || value > 0; ++i)
{
parts.Add(((char)(65 + (value % 26))).ToString());
value /= 26;
}
return string.Join(string.Empty, parts.AsEnumerable().Reverse().ToArray());
}
It will take the first 4 characters and use them as is, and then for the remainder of the value if will convert it into characters A-Z.
So 9999 becomes AAA9999, 10000 becomes AAB0000, and 270000 becomes ABB0000.
If the number is big enough that it exceeds 3 characters, it will add more letters at the start.
Here's an example of how you could go about implementing it
void Main()
{
string template = #"AAAA00";
var templateChars = template.ToCharArray();
for (int i = 0; i < 100000; i++)
{
templateChars = IncrementCharArray(templateChars);
Console.WriteLine(string.Join("",templateChars ));
}
}
public static char Increment(char val)
{
if(val == '9') return 'A';
if(val == 'Z') return '0';
return ++val;
}
public static char[] IncrementCharArray(char[] val)
{
if (val.All(chr => chr == 'Z'))
{
var newArray = new char[val.Length + 1];
for (int i = 0; i < newArray.Length; i++)
{
newArray[i] = '0';
}
return newArray;
}
int length = val.Length;
while (length > -1)
{
char lastVal = val[--length];
val[length] = Increment(lastVal);
if ( val[length] != '0') break;
}
return val;
}

Multiple string replace in c#

I am dynamically editing a regex for matching text in a pdf, which can contain hyphenation at the end of some lines.
Example:
Source string:
"consecuti?vely"
Replace rules:
.Replace("cuti?",#"cuti?(-\s+)?")
.Replace("con",#"con(-\s+)?")
.Replace("consecu",#"consecu(-\s+)?")
Desired output:
"con(-\s+)?secu(-\s+)?ti?(-\s+)?vely"
The replace rules are built dynamically, this is just an example which causes problems.
Whats the best solution to perform such a multiple replace, which will produce the desired output?
So far I thought about using Regex.Replace and zipping the word to replace with optional (-\s+)? between each character, but that would not work, because the word to replace already contains special-meaning characters in regex context.
EDIT: My current code, doesnt work when replace rules overlap like in example above
private string ModifyRegexToAcceptHyphensOfCurrentPage(string regex, int searchedPage)
{
var originalTextOfThePage = mPagesNotModified[searchedPage];
var hyphenatedParts = Regex.Matches(originalTextOfThePage, #"\w+\-\s");
for (int i = 0; i < hyphenatedParts.Count; i++)
{
var partBeforeHyphen = String.Concat(hyphenatedParts[i].Value.TakeWhile(c => c != '-'));
regex = regex.Replace(partBeforeHyphen, partBeforeHyphen + #"(-\s+)?");
}
return regex;
}
the output of this program is "con(-\s+)?secu(-\s+)?ti?(-\s+)?vely";
and as I understand your problem, my code can completely solve your problem.
class Program
{
class somefields
{
public string first;
public string secound;
public string Add;
public int index;
public somefields(string F, string S)
{
first = F;
secound = S;
}
}
static void Main(string[] args)
{
//declaring output
string input = "consecuti?vely";
List<somefields> rules=new List<somefields>();
//declaring rules
rules.Add(new somefields("cuti?",#"cuti?(-\s+)?"));
rules.Add(new somefields("con",#"con(-\s+)?"));
rules.Add(new somefields("consecu",#"consecu(-\s+)?"));
// finding the string which must be added to output string and index of that
foreach (var rul in rules)
{
var index=input.IndexOf(rul.first);
if (index != -1)
{
var add = rul.secound.Remove(0,rul.first.Count());
rul.Add = add;
rul.index = index+rul.first.Count();
}
}
// sort rules by index
for (int i = 0; i < rules.Count(); i++)
{
for (int j = i + 1; j < rules.Count(); j++)
{
if (rules[i].index > rules[j].index)
{
somefields temp;
temp = rules[i];
rules[i] = rules[j];
rules[j] = temp;
}
}
}
string output = input.ToString();
int k=0;
foreach(var rul in rules)
{
if (rul.index != -1)
{
output = output.Insert(k + rul.index, rul.Add);
k += rul.Add.Length;
}
}
System.Console.WriteLine(output);
System.Console.ReadLine();
}
}
You should probably write your own parser, it's probably easier to maintain :).
Maybe you could add "special characters" around pattern in order to protect them like "##" if the strings not contains it.
Try this one:
var final = Regex.Replace(originalTextOfThePage, #"(\w+)(?:\-[\s\r\n]*)?", "$1");
I had to give up an easy solution and did the editing of the regex myself. As a side effect, the new approach goes only twice trough the string.
private string ModifyRegexToAcceptHyphensOfCurrentPage(string regex, int searchedPage)
{
var indexesToInsertPossibleHyphenation = GetPossibleHyphenPositions(regex, searchedPage);
var hyphenationToken = #"(-\s+)?";
return InsertStringTokenInAllPositions(regex, indexesToInsertPossibleHyphenation, hyphenationToken);
}
private static string InsertStringTokenInAllPositions(string sourceString, List<int> insertionIndexes, string insertionToken)
{
if (insertionIndexes == null || string.IsNullOrEmpty(insertionToken)) return sourceString;
var sb = new StringBuilder(sourceString.Length + insertionIndexes.Count * insertionToken.Length);
var linkedInsertionPositions = new LinkedList<int>(insertionIndexes.Distinct().OrderBy(x => x));
for (int i = 0; i < sourceString.Length; i++)
{
if (!linkedInsertionPositions.Any())
{
sb.Append(sourceString.Substring(i));
break;
}
if (i == linkedInsertionPositions.First.Value)
{
sb.Append(insertionToken);
}
if (i >= linkedInsertionPositions.First.Value)
{
linkedInsertionPositions.RemoveFirst();
}
sb.Append(sourceString[i]);
}
return sb.ToString();
}
private List<int> GetPossibleHyphenPositions(string regex, int searchedPage)
{
var originalTextOfThePage = mPagesNotModified[searchedPage];
var hyphenatedParts = Regex.Matches(originalTextOfThePage, #"\w+\-\s");
var indexesToInsertPossibleHyphenation = new List<int>();
//....
// Aho-Corasick to find all occurences of all
//strings in "hyphenatedParts" in the "regex" string
// ....
return indexesToInsertPossibleHyphenation;
}

Increment a number within a string (C#)

I have a string with a number at the end, after a dash ("-"). I'd like to create that same string with that number incremented by 1. Pretty simple, but I'm wondering if there's a better approach to this? Thanks!
string oldString = "BA-0001-3";
int lastIndex = oldString.LastIndexOf("-");
string oldNumber = oldString.SubString(lastIndex + 1);
string oldPartialString = oldString.SubString(0, lastIndex);
int newNumber = Convert.ToInt32(oldNumber) + 1;
string newString = oldPartialString + newNumber.ToString();
Regex?
Example:
Regex.Replace("BA-0001-3", #"[A-Z]{2}-\d{4}-(\d+)",
m => (Convert.ToInt32(m.Groups[1].Value) + 1).ToString())
I would probably use my friend string.Split:
string oldString = "BA-0001-3";
string[] parts = oldString.Split('-');
parts[parts.Length-1] = (Convert.ToInt32(parts[parts.Length-1])+1).ToString();
string newString = string.Join("-", parts);
A small tweak that will perhaps make it quicker (by accessing parts.Length and subtracting 1 only once - didn't profile so it's purely a guess, and it is likely a marginal difference anyway), but above all more robust (by using int.TryParse):
string oldString = "BA-0001-3";
string[] parts = oldString.Split('-');
int number;
int lastIndex = parts.Length-1;
parts[lastIndex] = (int.TryParse(parts[lastIndex], out number) ? ++number : 1).ToString();
string newString = string.Join("-", parts);
Updated per Ahmad Mageed's comments below. This is his answer much more than it is mine now :-)
I would do it the way you have it now, but for fun wanted to see if I could do it with linq.
var x = "BA-0001-3".Split('-');
var y = x.First() + "-" + x.ElementAt(1) + "-" + (Convert.ToInt32(x.Last()) + 1);
This works in LINQPad.
Edit: Obviously I'm not a pro with linq. Hopefully there will be other answers/comments on how this can be improved.
Here's an example of how it could be done with RegEx:
public void Test()
{
System.Text.RegularExpressions.Regex rx = new Regex(#"(?<prefix>.*\-)(?<digit>\d+)");
string input = "BA-0001-3";
string output = string.Empty;
int digit = 0;
if (int.TryParse(rx.Replace(input, "${digit}"), out digit))
{
output = rx.Replace(input, "${prefix}" + (digit + 1));
}
Console.WriteLine(output);
}
Using the regex (which already seems to have now been filled in with more details) I end up with something like:
var regex = new Regex(#"^(?<Category>[A-Za-z]{1,2})-(?<Code>[0-9]{4})-(?<Number>[0-9]+)$");
var newCode = regex.Replace("BA-0001-3", new MatchEvaluator(ReplaceWithIncrementedNumber));
Where the MatchEvaluator function is:
public static string ReplaceWithIncrementedNumber(Match match)
{
Debug.Assert(match.Success);
var number = Int32.Parse(match.Groups["Number"].Value);
return String.Format("{0}-{1}-{2}", match.Groups["Category"].Value, match.Groups["Code"].Value, number + 1);
}
Here is an example of a class that exposes the three parts of your "part number". Not particularly fancy (also note the absence of error checking/validation).
class Program
{
static void Main(string[] args)
{
PartNumber p1 = new PartNumber("BA-0001-3");
for (int i = 0; i < 5; i++)
{
p1.Sub++;
Debug.WriteLine(p1);
}
PartNumber p2 = new PartNumber("BA", 2, 3);
for (int i = 0; i < 5; i++)
{
p2.Sub++;
Debug.WriteLine(p2);
}
}
}
class PartNumber
{
public PartNumber(string make, int model, int sub)
{
Make = make;
Model = model;
Sub = sub;
}
public PartNumber(string part)
{
//Might want to validate the string here
string [] fields = part.Split('-');
//Are there 3 fields? Are second and third fields valid ints?
Make = fields[0];
Model = Int32.Parse(fields[1]);
Sub = Int32.Parse(fields[2]);
}
public string Make { get; set; }
public int Model { get; set; }
public int Sub { get; set; }
public override string ToString()
{
return string.Format("{0}-{1:D4}-{2}", Make, Model, Sub);
}
}

attempting to create string of random values

I'm trying to generate a string of a random length which consists out of random chars.
To do so I have this code:
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 1000; i++)
{
MyString test = new MyString();
test.Random();
Console.WriteLine(test.Value);
}
Console.ReadLine();
}
}
public class MyString
{
private string value = string.Empty;
private Random r = new Random();
public string Value
{
get { return this.value; }
set { this.value = value; }
}
public void Random()
{
int length = (r.Next() % (100)) + 1;
for(int i = 0; i < length; i++)
{
value = value + RandomChar();
}
}
public char RandomChar()
{
// 32 to 126
int c = (r.Next() % 95) + 32;
char ch = (char)c;
return ch;
}
}
Now, lets look at a part of the output:
As you can see, the output is far from random, it contains a lot of repeating strings. How is this possible, and how do I solve it?
It looks like you are creating a new instance of the Random class every time your MyString constructor is called. The Random class probably seeds itself based on the current time (to some resolution). Random number generators seeded with the same value will generate the same pseudo-random sequence.
The solution is to construct one instance of Random and use that everywhere.
http://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename.aspx
string randomName = Path.GetRandomFileName();
randomName = randomName.Replace(".", string.Empty);
// take substring...

Categories