2

I have a lot of string like this "01/08/2019" and i want to have a string like this "20190801" . I cant use the DateTime format , i must do it using string as a type . Please someone to help

IMAD HAMMANI
  • 115
  • 1
  • 1
  • 4
  • You should try to break the entire problem down into smaller problems. Your first task (and you search query for your search engine) here could be "How to split the string in the single parts" this would let you find [this post](https://stackoverflow.com/questions/8928601/how-can-i-split-a-string-with-a-string-delimiter). The second subproblem is "how to reverse the order of elements in an array" => which might lead to [this post](https://stackoverflow.com/a/18394946/5174469) then you need only to glue the parts together ;) good fortune – Mong Zhu Jul 29 '19 at 05:04
  • No its not a homework of course . Its for my web Application , i cant use the Date Time format because i m getting the info from a Database , and in the database the type was string . Thats why i must keep the string type . Concerning the code , i ve tried some methods but they didnt work at all so i didnt share them ! sorry about it – IMAD HAMMANI Jul 29 '19 at 16:46
  • ah that sounds legit, then the simplest solution seems to be to parse it into a `DateTime` and use the `ToString` method to create a string in the desired format, like @Adassko did. – Mong Zhu Jul 30 '19 at 04:53
  • @IMADHAMMANI if the answer is correct propose it as correct answer that help other people find the solution with same problem. – Parsa Karami Jul 31 '19 at 05:49

5 Answers5

3

You can use this

string date = "01/08/2019";
string result = string.Empty;
foreach(var item in date.Split('/'))
    result = string.Concat(item, result);
Parsa Karami
  • 702
  • 1
  • 8
  • 30
1

what do you mean that you cannot use DateTime format? Normally you should parse the format, keep it as DateTime in memory and use .ToString(format) for presentation purposes. Doing it all in one line would look like this:

DateTime.ParseExact("01/08/2019", "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("yyyyMMdd")
Adassko
  • 5,201
  • 20
  • 37
1

if you surely have '/' as separator,

you can split the string with '/', by this you will get string array.

you can loop on this array in descending order and keep concatenating the element of the array to form one single output.

string dateStr = "01/01/2019";
string[] dateElements = dateStr.Split('/');
string output = string.Empty;
for(int i = dateElements.Length - 1; i >= 0; i--)
{
     output += dateElements[i];
}
Amit
  • 1,821
  • 1
  • 17
  • 30
0

You can write that code like that:

var input = @"01/08/2019";
var chrs = new[] {'/'};
var result = string.Concat(input.Where(c => !chrs.Contains(c)));
Prochu1991
  • 443
  • 5
  • 20
  • sorry mate but this yield also the wrong output. You could also include an explanation of what this code does. It will help OP and future readers. – Mong Zhu Jul 29 '19 at 05:17
0

This way:

class Program
{
    static void Main(string[] args)
    {
        string str = "01/08/2019";
        string normalizedStr = Normalize(str);
    }

    private static string Normalize(string str)
    {
        return string.Join("-", str.Split(new char[] { '/' }).Reverse());
    }
}

Basically it split the original string in many strings by char '/'

Then it reverse the order of these strings

Then it concatenate by using "-" as separator

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32