2011-12-01 62 views

回答

9
string myString = "0001-102525"; 
string[] splitString = myString.Split("-"); 

然後訪問要麼像這樣:

splitString[0]splitString[1]

唐別忘了c如果你正在分割用戶輸入的字符串,因爲它們可能沒有輸入' - ',這會導致OutOfRangeException

5

您可以使用C#拆分方法 - MSDN

8

如何:

string[] bits = text.Split('-'); 
// TODO: Validate that there are exactly two parts 
string first = bits[0]; 
string second = bits[1]; 
0
string strData = "0001-102525"; 
//Using List 
List<string> strList = strData.Split('-').ToList(); 
string first = strList.First(); 
string last = strList.Last(); 

//Using Array 
string[] strArray = strData.Split('-'); 
string firstItem = strArray[0]; 
string lastItem = strArray[1]; 
0
string strToSplit = "0001-102525"; //Can be of any length and with many '-'s 
string[] arrStr = strToSplit.Split('-'); 
foreach (string s in arrStr) //strToSplit can be with many '-'s. This foreach loop to go thru entire arrStr string array 
{ 
    MessageBox.Show(s); 
} 
0
string myString = "0001-102525"; 

//to split the string 

string [] split = myString.Split("-"); 

//to display the new strings obtained to console 

foreach(string s in split) 
{ 
    if(s.Trim() !="") 
    Console.WriteLine(s); 
}