C# program to replace all commas with newline in a string

C# program to replace all commas with newline in a string:

This post will show you how to replace all commas with newlines in C#. We will write one C# program that will ask the user to enter a string. It will replace all commas with newlines and print that string.

With this post, you will learn how to read user input string and how to replace a character in a string in C#.

C# program:

Below is the complete C# program:

using System;

public class Program
{
	public static void Main()
	{
		string str;
        
        Console.Write("Enter a string : ");
        str = Console.ReadLine();
        
        String modifiedString = str.Replace(',','\n');
		Console.WriteLine("New string : ");
		Console.Write(modifiedString);
	}
}

Here,

  • We are using Replace to replace the , with newline \n character.
  • It returns the newly created string and it is storing that value in the modifiedString variable.
  • At the end, it prints that value.

Sample output:

It will print output as like below:

Enter a string : hello,world,!!
New string : 
hello
world
!!

You might also like: