string split and read lines

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		//Pretend this came from File.ReadLines()
		IEnumerable<string> lines = new [] { "name:Steve", "username:steveiscool", "title:the boss", "phone:5556789" };
		
		//Iterates over the lines variable
		foreach(string line in lines)
		{
			//splits each line on the ':' character to form a string array of all the "parts"
			string[] split = line.Split(':');
			
			for(int i = 0; i < split.Length; i++)
			{
				//Writes the index of the array, and the value at that index
				Console.WriteLine("{0}: '{1}'", i, split[i]);
			}
			Console.WriteLine("----------------------");
		}		
	}
}
;