Program to Split String in C#

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 1423
Suppose we have a text as

Please ignore this line
UserName: RNA Team
Country: India


We need to get the UserName and Country value. The below program will do so

using System;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

var inputText = @"Please ignore this line
UserName: RNA Team
Country: India";
inputText
.Split(new string[] { "\r\n" }, StringSplitOptions.None)
.Where(s => s.IndexOf(':') != -1) //pick up those lines where : is present
.Select(s => s.Trim()) //remove the unwanted white spaces
.ToList()
.ForEach(line =>
{
var txt = Regex.Split(line, @":");
Console.WriteLine("{0} : {1}", txt[0], txt[1]);
});

System.Console.ReadKey();

}
}

}

/*

Result
--------
UserName : RNA Team
Country : India


*/

Comments or Responses

Login to post response