Search code examples
stringsplitreformat

Split/Reformat String


I want to split a string and replace some values. The string is a date that looks like this '08/26/2009' I want to reformat it so that it replaces the / with a - so that it looks like this '08-26-2009'

The string is passed into a variable y, so my line looks like this

test = y.split("/")
print y

return this ['8', '26', '2009']

I just can't figure out how to reformat it to place the dash between the day month and year.

Thanks Mike


Solution

  • You can use replace instead of split.

    test = y.replace('/', '-');
    

    (assuming this is Java)