Cloudmersive

Apr 17, 2020

2 min read

How to split a single Text File TXT into Lines in C# .NET Framework

Having proper methods for structuring text can be a crucial feature in many projects. In this quick tutorial, I’m going to demonstrate a criminally easy way to implement such a method for splitting a text into lines using C#. Let’s dive straight in.

We begin with the installation of our API client for documents and conversion. Run this command from your console in package manager to continue.

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 2.2.0

Next comes the SplitDocumentTxtByLine function. We can use the following chunk of code to call it.

using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;namespace Example{public class SplitDocumentTxtByLineExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new SplitDocumentApi();var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.try{// Split a single Text file (txt) into linesSplitTextDocumentByLinesResult result = apiInstance.SplitDocumentTxtByLine(inputFile);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling SplitDocumentApi.SplitDocumentTxtByLine: " + e.Message );}}}}

Well, that was easy. If you drop in a TXT file you can give it a quick test. But apart from that, our work here is done! Be sure to look throught the documentation section for more like this!