How to remove specific characters from a string in C#

1 Answer

0 votes
using System;

public class Program
{
	public static string removeSpecificCharactersFromString(string str, char[] charsToRemove) {
		foreach (char ch in charsToRemove) {
			str = str.Replace(ch.ToString(), "");
		}

		return str;
	}

	public static void Main(string[] args)
	{
		string phone = "(555) 555-5555";
		char[] charsToRemove = new char[] {'(', ')', '-'};

		phone = removeSpecificCharactersFromString(phone, charsToRemove);

		Console.WriteLine(phone);
	}
}



/*
run:
  
555 5555555
  
*/



 



answered Mar 17, 2024 by avibootz

Related questions

1 answer 139 views
3 answers 136 views
1 answer 142 views
1 answer 168 views
1 answer 131 views
...