Replace all Characters in a String

I’m using Replace is order to replace all characters in a string with ‘-’. However, I do not know a way to replace all characters with dashes, only those that are specified.
Here’s the code:

ans = wordGen.Replace ("a", "-");

Is there any way to get it to replace all characters in the string, instead of just ‘a’?

Thanks in advance

-MrFodds

The straightforward way would be something like this (replace the old string with a new string of the same length composed of only the desired character):

string ReplaceAll(string input, char target)
{
	StringBuilder sb = new StringBuilder(input.Length);
	for(int i = 0; i < input.Length; i++)
	{
		sb.Append(target);
	}
	
	return sb.ToString();
}

Then implementation would be:

ans = ReplaceAll(wordGen, '-');

You could even go the fancy way and add an overload that takes a char list as filter, so you can do something like this:

string ReplaceAll(string input, char target, List<char> filter)
{
	StringBuilder sb = new StringBuilder(input.Length);
	for(int i = 0; i < input.Length; i++)
	{
		if(filter.Contains(input*))*
  •  {*
    

_ sb.Append(input*);_
_
}_
_
else*_
* {*
* sb.Append(target);*
* }*
* }*

* return sb.ToString();*
}
Note that for these methods you need to import these namespaces:
using System.Text;
using System.Collections.Generic;

You can create an array of characters with the same length as the string, use a for loop to fill it with dashes and put that back into a string. This would replace everything but spaces:

string text = "Some text!";

char[] dashes = new char[text.Length];
for(int i=0; i<dashes.Length; i++){
    dashes _= text *== ' '? ' ':'-';*_

//or dashes = ’ '; for every character
}
string output = new string(dashes);
//output would be “---- -----”