Write to text file [C#]

c# write to text file
File class in System.IO Namespace also contains methods to write contents to text tile. WriteAllText() method to write string to text file. WriteAllLines() method is used to write line by line into text file. And WriteLine() method writes or appends text to the text file with existing contents.
How to write to a text file?
TO write to a text file in file system .net has provided below methods in File class of System.IO namespace:
1.WriteAllText.
2.WriteAllLines.
3.WriteLine.

1.WriteAllText.

WriteAllText method in File class writes one string to the text file

Sample code that shows how to write into to text file[C#]

class WriteFileAllText
{
 static void Main()
 {
  // Example to Write one string into text file.
   string textToWrite = "To write one string into a text file use 
   WriteAllText method, " ;
   System.IO.File.WriteAllText(@"C:\\TextFileFolder\NewTextFile.txt", textToWrite);
 }
}  

2.WriteAllLines.

WriteAllLines method in File class writes contents to a text file line by line.

Sample code that show how to write into a text file line by line[C#]

class WriteFileLines
{
 static void Main()
 {  
  // Example to Write the contents line by line into text file.
  string[] lines = {"Line One", "Line Two", "Line Three"};
  System.IO.File.WriteAllLines(@"C:\TextFileFolder\WriteLines.txt", lines);}
 }  

3.WriteLine.

WriteLine method in File class writes or appends line to a text file that already have some data.

Sample code that show how to append text line into a text file.[C#]

It uses StreamWriter class of System.IO namespace to append line in existing text file
class WriteFileLine
{
static void Main()
{  
 // Append text line to existing file
 System.IO.StreamWriter file = new System.IO.StreamWriter ("C:\TextFileFolder\WriteLines.txt", true)
 {
  file.WriteLine("Line Four");
 }
}

C# Uppercase First Character or Letter

uppercase first letter in string

Some times we need to uppercase the first letter. There are various ways to make first word in uppercase. we will look at some of the ways to uppercase the first letter in strings in the C#. We can take out first character of string from it’s zero index and the using char.ToUpper(str[0]) we can capitalize first character and then concatenate it with remaining string and make string again. Other way to uppercase first character or letter is use .ToCharArray() method and then from array of characters take zero index character make it uppercase and then formulate new string again. Other alternative is to use Substring() method and take characters from position 0 to 1 and capitalize first character and then using substring from 1 position concatenate those strings.

Here we will see how first way will work to uppercase first character or letter of the string using c#.

C# code to uppercases first character

using System;
class Program 
{ 
 static void Main()
 {
  Console.WriteLine(UppercaseFirstLetter("first")); 
 }
 static string UppercaseFirstLetter(string str)
 {
  var  firstChar = str[0];
  var  UpperCaseFirstCharacter =  char.ToUper(firstChar);
  var convertedFirstCharToUpper = UpperCaseFirstCharacter +  str.Substring(1);
  return convertedFirstCharToUpper ;
 }
}


//Result:
//First.

In above code sample for upper case first letter we first take first char as str[0] and using char.ToUpper(str[0]) we Capitalized first character and then concatenated remaining string with first capitalized char and the returns the new string with first letter capitalized.


Second way to Uppercase the First latter of the String:


C# code to uppercases first letter


using System;

class Program
{
 static void Main()
 {
  Console.WriteLine(UppercaseFirstCharacter("first"));    
 }

 static string UppercaseFirstCharacter(string str)
 {
  char[] strArray = str.ToCharArray();
  strArray[0] = char.ToUpper(strArray [0]);
  return new string(strArray);
 }
}


//Result
//First.

In above C# code we have converted input string in to array of characters using .ToCharArray() and then took zero index character and uppercase it and the formed new string with new string constructor to construct string.

Third way to uppercase first character of string using c#

C# code to uppercases first character [C#]


public static string Capitalize(string input)   
{
 str = "first";
 return str.Substring(0, 1).ToUpper() + str.Substring(1);
}


//Result:
//First

In above C# example to uppercase first character of string we used Substring() method. using Substring(0,1) we get first character of string and then we capitalized that character like Substring(0,1).ToUpper() and then we concatenated remaining string from first position onwards to it.