2016-04-10 6 views
0

私は、ユーザーからの日付入力を格納する配列を持っているが、私は削除または配列で入力されたすべての日から(基本的に、私は月を削除する)最初の2つの文字をトリミングしたい:配列の日付文字列(MM/dd/yyyy)から最初の2文字を削除するにはどうすればよいですか?

class MainClass 
{ 
    { 
    //Main Program.... 
    } 

public static int GetInput (string[] date) 
{ 
    int loop; 

    (for int i=0 ; i < loop ; i++) 
    dArray[i] = Console.ReadLine(); 
} 
} 

class OtherClass 
{ 
    //Required data properties, etc... 

    public string TrimFirstTwoMonthChar(string dateInput) 
    { 
     char[] delimiter = {'/', '-', .... } 

     string[] monthNumberRemoved = dateInput.Split(delimeter); 

    // How would I code the rest of this function so that it removes the first 2 characters from "MM/dd/yyyy". 
    //Keep in mind I have also allowed users to input the date in formats like 
    //"M/dd/yyyy" (such as 3/07/2011 vs 03/07/2011) 
    //so sometimes I would only need to remove ONE NOT TWO of the month character // 
    } 
} 
+0

あなたはコメントの説明に言い直す必要があります。論理的に考えると、最初の2文字<10の値なら 'm/dd/yyyy 'else' mm/dd/yyyy'はもちろん部分文字列関数を使って長さなどを調べます。 – MethodMan

+0

(int i = 0; i llouk

答えて

0

、あなたは、単純なストリングを使用することができます

public static string TrimFirstTwoMonthChar(string dateInput) 
{ 
    var indexOfFirstBar = dateInput.IndexOf('/'); 
    var start = indexOfFirstBar + 1; 

    return dateInput.Substring(start, dateInput.Length - start); 
} 

をしかし、私は、あなたがたDateTimeに変換して、必要な日付形式を使用することをお勧め:Convert.ToDateTimeについて

public static string TrimFirstTwoMonthChar(string dateInput) 
{ 
    var date = Convert.ToDateTime(dateInput); 
    return date.ToString("dd/yyyy"); // Use the format you want here 
} 

をし、 date formats

0

何かのようにこの?文字列で

public string TrimFirstTwoMonthChar(string dateInput) 
{ 
    char[] delimiter = {'/', '-', .... } 

    string[] monthNumberRemoved = dateInput.Split(delimeter); 

    return monthNumberRemoved[1] + "/" + monthNumberRemoved[2]; 
} 
+0

ありがとう!それを私には起こさなかった。 Trim、Remove、またはSubstringを使用するルートに沿ってもっと考えていましたが、これも同様に機能します。 – 5120bee

関連する問題