0

I have some c# code like this:

string myString = "20180426";

I know how to parse around specific characters (using the string.Split thing), but how do I get it to return 3 strings like this:

2018
04
26

I have several strings that are formatted this way ("YYYYMMDD"), so I don't want code that will only work for this specific string. I tried using

var finNum = myString[0] + myString[1] + myString[2] + myString[3];
Console.Write(finNum);

But I guess it's treating the characters as integers, rather than a text string because it's doing some mathematical operation with them instead of concatenating (it's not addition either because it's returning 203, which isn't the sum of 2, 0, 1 and 8).

I've tried changing var to string, but it won't let me implicitly convert int to string. Why does it think that string myString is an int, rather than a string, which is what I declared it as?

I could also use DateTime.Parse and DateTime.ParseExact, but apparently "20180426" isn't recognized as a valid DateTime:

DateTime myDate = DateTime.ParseExact(myString, "YYYYMMDD", null);
Console.WriteLine(myDate);

Thank you for your help. I know the answer is probably stupidly easy and I feel dumb for asking but I seriously checked all over various websites and can't find a solution that works for my issue here.

5
  • 2
    You used the wrong formatting string. It should be yyyyMMdd. YYYY and DD are not recognized as valid format characters, they are treated as string literals Commented Dec 13, 2018 at 16:14
  • "but apparently "20180426" isn't recognized as a valid DateTime" insert this char "-" in the string. 2018-04-26 and try to parse with DateTime.Parse again. Commented Dec 13, 2018 at 16:18
  • About the interger transformation: var finNum = myString[0] + myString[1] + (...) ; sums the ASCII values of the corresponding chars. If you add a string transformation (you need to use it just on the first char): var finNum = myString[0].ToString() + myString[1] + (...) ; it will switch to string concatenation. Commented Dec 13, 2018 at 16:20
  • 2
    @Paulo there's nothing wrong with the unseparated format Commented Dec 13, 2018 at 16:22
  • As for var finNum = myString[0] + myString[1] + myString[2] + myString[3]; - myString[1] returns a character - if you try to do mathical operations with a char it will calculate the integer representation of the char so your var finNum is actually an int as you can see here: dotnetfiddle.net/tP9nRU Commented Dec 13, 2018 at 16:22

2 Answers 2

3

I could also use DateTime.Parse and DateTime.ParseExact, but apparently "20180426" isn't recognized as a valid DateTime.

Yes, because the format string YYYYMMDD is incorrect, years and days are lowercase:

DateTime myDate = DateTime.ParseExact(myString, "yyyyMMdd", null);

If you want the year, month and day:

int year = myDate.Year;
int month = myDate.Month;
int day = myDate.Day;
Sign up to request clarification or add additional context in comments.

3 Comments

DERP I feel really dumb. Thank you so much. By the way, is there a way to return only the first 4 characters of a string or, like characters 5, 6 and 7 of a string? I looked all over the internet and couldn't find that answer, though I'm sure it's out there somewhere.
@TnD_Guy: Yes, Substring you should use. But don't use string methods if you're working with DateTimes. Parse it and then do something with it. As shown in my answer a DateTime has also properties to get the month for example.
@PanagiotisKanavos I'm very new to programming in general. I'm still not 100% sure what a method is. I've read some tutorials and stuff, but it's a lot of info for someone who has never messed with programming before. I have so much to learn, and I'm getting there, but I've only been doing this for roughly 4 weeks or so. I do appreciate the help, though. Thank you!
-1

If you want year, month and day separated by variables you could try:

       string mystring = "20180426";
       mystring = mystring.Insert(4,"-");
       mystring = mystring.Insert(7,"-");

       string year = mystring.Split('-')[0];
       string month = mystring.Split('-')[1];
       string day = mystring.Split('-')[2];
  1. First I add a character "-" to separate year and month, then another to separate month and day. You get something like "2018-04-26"

  2. Then I split the string and save the position 0 that store the first 4 numbers of your string into a variable named year.

Good luck!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.