locked
split string RRS feed

  • Question

  • User-2060576634 posted

    hi.. is there a way to split this year-month string using a dash like below?

    201509  => 2015-09

    Tuesday, June 2, 2015 7:11 PM

Answers

  • User281315223 posted

    I suppose you could just write a function to handle formatting the two specific sections (e..g the first four characters, a dash, then the last two characters) :

    var input = "201509";
    var output = String.Format("{0}-{1}",input.Substring(0,4),input.Substring(4)); // yields "2014-09"
    

    There are quite a few ways to handle this (using a Regular Expression, parsing your string as a DateTime object and then formatting it, etc.), but this might be the most straightforward. You can see a working example of it here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 3, 2015 12:07 AM