2016-11-07 35 views
-3

我需要幫助。試圖從字符串中取出第二個字符時,我該怎麼做?我想從字符串中取出一個字符

// read in a string from the user in the following format: 
//pyramid slot number, block letter, whether or not the block should be lit 
Console.Write("Enter slot number, block letter & if the block should be lit or not (y/n)"); 
string csvString = Console.ReadLine(); 

// find comma location 
int commaLocation = csvString.IndexOf(','); 

// extract slot number 
int slotnumber = int.Parse(csvString.Substring(0,commaLocation)); 

// Print slot number 
Console.WriteLine("Slot number: " + slotnumber); 

// extract block letter 
*string blockletter = string.ConvertToChar(csvString.Substring(commaLocation + 1));* 
      // print block letter 
Console.WriteLine("Block letter: " + blockletter); 
+0

你必須在ascii值混淆? –

回答

0

您可以使用Split()將字符串分隔成單詞/字符列表。

List<string> charactersSplit = csvString.Split(","); 
string slotNumber = charactersSplit[0]; 
string blockLetter = charactersSplit[1]; 
+0

教授意味着我們不應該使用字符串,但字符。有問題發生的地方:-)謝謝你的幫助和興趣 –

相關問題