0

So I wanted to make a random timestamp generator in c# which had to be a string.

I came up with a random generator made out of loads of random.next's but when the day month hour or minute is under 10 it has to have a zero in front, or the string length won't reach the length I need it to be which is like this:

1111/11/11 11:11

without spaces and slashes so it'll be like this:

111111111111

but if the minute is like 9 then it'll be like this

11111111119

which is not long enough!

here is my code now

decimal cijfer;
string tijd;
string jaar;
string maand;
string dag;
string uur;
string minuten;

Random random = new Random();

cijfer = random.Next(64, 94);
jaar = random.Next(1943, 2020).ToString();
maand = random.Next(0, 13).ToString();
dag = random.Next(0, 29).ToString();
uur = random.Next(0, 23).ToString();
minuten = random.Next(0, 61).ToString();

maand.PadLeft(2, '0');
dag.PadLeft(2, '0');
uur.PadLeft(2, '0');
minuten.PadLeft(2, '0');

tijd = jaar + maand + dag + uur + minuten;

Console.WriteLine(tijd);
Console.ReadLine();

which does not work and I do not understand why.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
Swift
  • 21
  • 8
  • 4
    `PadLeft` returns a new string. It does not mutate the string that's passed in. You need to assign the result of your padding. – Jonathon Chase Feb 06 '19 at 23:07
  • 2
    @JonathonChase is correct. Also, that applies to **all** functions that interact with strings. A `string` is never altered (unless you do something dodgy like `unsafe`) since they are **immutable**. So if you ever call **any** function on a string it will almost certainly _seem to_ do nothing unless you do something with the **result** of the function call. – mjwills Feb 06 '19 at 23:13
  • 1
    [Duplicate](https://stackoverflow.com/questions/1948978/string-replace-or-other-string-modification-not-working) covers why code you have does not work (string modification functions returns new value), but what you really looking for is https://stackoverflow.com/questions/4325267/c-sharp-convert-int-to-string-with-padding-zeros - `String.Format("{0:D4}{1:D2}...", random.Next(1943, 2020), random.Next(0, 13)…` – Alexei Levenkov Feb 06 '19 at 23:35

1 Answers1

0

String.PadLeft Method

Returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character.

You will need to modify to take the new string into account

maand = maand.PadLeft(2, '0');
dag = dag.PadLeft(2, '0');
uur = uur.PadLeft(2, '0');
minuten = minuten.PadLeft(2, '0');

tijd = jaar + maand + dag + uur + minuten;
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • already fixed it like this: – Swift Feb 06 '19 at 23:26
  • string maandd = maand.PadLeft(2, '0'); string dagg = dag.PadLeft(2, '0'); string uurr= uur.PadLeft(2, '0'); string minutenn = minuten.PadLeft(2, '0'); tijd = jaar + maandd + dagg + uurr + minutenn; – Swift Feb 06 '19 at 23:26
  • but thanks! :) also sorry for the multiple posts I thought I could just enter to the next sentence... – Swift Feb 06 '19 at 23:27