//read
Console.Write("Please enter (pyramid slot number,block letter,whether or not the block should be lit): ");
string csvString = Console.ReadLine();
//location of comma
int firstComma = csvString.IndexOf(',');
int secondComma = csvString.IndexOf(',', firstComma + 1);
//extract slot number of pyramid
int slotNumber = int.Parse(csvString.Substring(0, firstComma));
string blockLetter = csvString.Substring(firstComma + 1, secondComma);
Boolean lit = Boolean.Parse(csvString.Substring(secondComma + 1));
//print
Console.WriteLine("Pyramid Slot Number: " + slotNumber);
Console.WriteLine("Block Letter: " + blockLetter);
Console.WriteLine("Lit: " + lit);
I tried to input like "5,M,true". However output for Block Letter is "M,t". If I try to input 15 instead of 5, then it gives "M,tr". In the end, I want to get only one letter. I'll use char after I figure this problem out.
Edit:
char blockLetter = char.Parse(csvString.Substring(firstComma + 1, 1));
I used this thank you!
The first parameter of String.Substring
is the start index, the second parameter is not the end index but the length. So you need to calculate it:
int firstComma = csvString.IndexOf(',');
int startIndex = firstComma + 1;
int secondComma = csvString.IndexOf(',', startIndex);
int length = secondComma - startIndex;
string blockLetter = csvString.Substring(startIndex, length);
An easier way is to use String.Split
to get a string[]
with all tokens delimited by comma:
string[] allSlots = csvString.Split(',');
// first token is in allSlots[0] and second in allSlots[1]