2017-03-09 61 views
0

假設您有一個字符串5/9/2010,並且您想重新排列它以讀取2010/5/9。你會怎麼做呢?Unity3d重新排列字符串

我想通過恰好是日期的字符串對列表進行排序。儘管我可以把它變成一個日期,但如果可能的話,我想堅持使用一個字符串,因爲日期時間的一部分很難消除。 (這是一個用於Unity3d應用程序的sqlite數據庫。) 請原諒我,如果這是重複的。

回答

2

如果你能保證該字符串將永遠是相同的輸入格式,可以拆分對/字符串:

string input = "5/9/2010"; 
    string[] inputSections = input.Split('/'); 
    string output = string.Format("{0}/{1}/{2}", inputSections[2], inputSections[0], inputSections[1]); 

Working Fiddle

我的代碼是非常詳細的,你當然可以簡化它符合你的需求。我也將利用C#的6 string inerpolation功能,如果它是提供給你:

string input = "5/9/2010"; 
    string[] inputSections = input.Split('/'); 
    string output = $"{inputSections[2]}/{inputSections[1]}/{inputSections[0]}"; 
+0

謝謝!看起來這會很好地工作! –

1

我會建議解析之日起,在關機會,該輸入的日期是不完全在您所期望的格式,但確實是一個有效的日期。這是解析的目的。

CultureInfo us = CultureInfo.GetCultureInfo("en-US"); 
string input = "5/9/2010"; 
DateTime date = DateTime.Parse(input, us); 

Console.WriteLine(date.ToString("yyyy/MM/dd", us)); 

您可以測試here