locked
use substring in c #???

    Question

  • please i have this string

    12-1-2010 as string

    string date = "12-1-2010"

    how can i get only the year in separate string

    and month in separate one and day in separate one

    please help

    Wednesday, January 26, 2011 8:36 AM

Answers

  • Split the string based on - character and that will produce an array of three elements each of which contains the information you seek.

    "12-1-2010".Split( ... )

     

    Look at the help files, press F1 when the cursor is on the Split method, real easy mate, Give it a go and if you can't get it working post back with what you code have and we'll sort it.


    …we each have more potential than we might ever presume to guess. (Blog: http://dsmyth.blogspot.com/)
    • Marked as answer by abu5alilo Wednesday, January 26, 2011 8:55 AM
    Wednesday, January 26, 2011 8:43 AM
  • If you want to use a substring method, you can do it this way:

    string date = "12-1-2010";
    string year = date.Substring(date.Length - 4, 4);
    

    This way you will always get the last 4 characters - so the year ("2010").

    Hope it helps

     

    • Marked as answer by abu5alilo Wednesday, January 26, 2011 10:13 AM
    Wednesday, January 26, 2011 10:08 AM

All replies

  • Split the string based on - character and that will produce an array of three elements each of which contains the information you seek.

    "12-1-2010".Split( ... )

     

    Look at the help files, press F1 when the cursor is on the Split method, real easy mate, Give it a go and if you can't get it working post back with what you code have and we'll sort it.


    …we each have more potential than we might ever presume to guess. (Blog: http://dsmyth.blogspot.com/)
    • Marked as answer by abu5alilo Wednesday, January 26, 2011 8:55 AM
    Wednesday, January 26, 2011 8:43 AM
  • hi man

    thanks

    if i have a sting Month = "10"?

    how can i get month name ?

    Wednesday, January 26, 2011 9:04 AM
  • Put the bits of the date into a DateTime and use the ToString method to return the date formatted; have a look at the DateTime.ToString() method as there are a lot of options for producing different date outputs.

                DateTime example = new DateTime(2010, 10, 12, 0, 0, 0);
                Console.Out.WriteLine(example.ToString("MMMM"));


    …we each have more potential than we might ever presume to guess. (Blog: http://dsmyth.blogspot.com/)
    Wednesday, January 26, 2011 9:26 AM
  • thanks but i mean

    i need to fill months name in ddlMonthes  ( dropdownlist )

    using for loop

    for ( int m = 1 ; m <=12 ; m++ )

    {

    ddlMonths.datatextfields= """""""" >>> the month name

    ddl.months.datavaluefields = m ;

    }

    Wednesday, January 26, 2011 10:04 AM
  • If you want to use a substring method, you can do it this way:

    string date = "12-1-2010";
    string year = date.Substring(date.Length - 4, 4);
    

    This way you will always get the last 4 characters - so the year ("2010").

    Hope it helps

     

    • Marked as answer by abu5alilo Wednesday, January 26, 2011 10:13 AM
    Wednesday, January 26, 2011 10:08 AM
  •  //to get day, month and a year seperately:
          string[] data = date.Split('-');
          string day = data[0].Trim();
          string month = data[1].Trim();
          string year = data[2].Trim();
    
    Wednesday, January 26, 2011 10:10 AM
  • The CultureInfo.CurrentCulture.DateTimeFormat.MonthNames property returns an array containing the month names.
    Wednesday, January 26, 2011 4:29 PM