none
Reverse 2 sets of characters position in string ? RRS feed

  • Question

  • Greetings ,

    Imagine a string which contains a date like : 30/05/2018

    I want to change the position of year (2018) with day(30) , It should be like : 2018/05/30

    What to do suggest ?

    Thanks

    Monday, October 1, 2018 12:16 AM

Answers


  • Imagine a string which contains a date like : 30/05/2018

    I want to change the position of year (2018) with day(30) , It should be like : 2018/05/30


    One approach is to split the string on the / characters. Using the 
    String.Split method will create an array of strings holding for example

    30
    05
    2018

    You can then build a new string by concatenating these strings in the
    order you want, with / characters where appropriate. The & operator
    should suffice for that.

    - Wayne

    • Marked as answer by Kevin993 Monday, October 1, 2018 1:04 AM
    Monday, October 1, 2018 12:41 AM

All replies

  • Once you make a DATE variable you can list it many ways.
     ' create a DATE variable from that string in a known format:
            Dim newDate As Date = DateTime.ParseExact("30/05/2018", "dd/MM/yyyy", Globalization.CultureInfo.InvariantCulture)
    
            'output 30/05/2018
            Console.WriteLine(newDate.ToString("dd/MM/yyyy"))
    
            'output 2018/05/30
            Console.WriteLine(newDate.ToString("yyyy/MM/dd"))


    Monday, October 1, 2018 12:41 AM

  • Imagine a string which contains a date like : 30/05/2018

    I want to change the position of year (2018) with day(30) , It should be like : 2018/05/30


    One approach is to split the string on the / characters. Using the 
    String.Split method will create an array of strings holding for example

    30
    05
    2018

    You can then build a new string by concatenating these strings in the
    order you want, with / characters where appropriate. The & operator
    should suffice for that.

    - Wayne

    • Marked as answer by Kevin993 Monday, October 1, 2018 1:04 AM
    Monday, October 1, 2018 12:41 AM
  • Once you make a DATE variable you can list it many ways.
     ' create a DATE variable from that string in a known format:
            Dim newDate As Date = DateTime.ParseExact("30/05/2018", "dd/MM/yyyy", Globalization.CultureInfo.InvariantCulture)
    
            'output 30/05/2018
            Console.WriteLine(newDate.ToString("dd/MM/yyyy"))
    
            'output 2018/05/30
            Console.WriteLine(newDate.ToString("yyyy/MM/dd"))


    I know that we can list a date string in different ways , but I mentioned date just as an example of the string. It might be 5565/156/2231
    Monday, October 1, 2018 12:47 AM