Replace whole characters in a string with one character

How can I change a string that only contains 1 character? An example of how to change oldString to newString:

oldString = "Hello, world!";
newString = "-------------";

oldString = "Unwanted";
newString = "--------";

      

+3
string c # replace


source to share


1 answer


As we all know, a string is a set of characters, so the constructor of the String class will help you create a string by repeating a character N

once. Here we can use this constructor to create. You can try like this:

string oldString = "Hello, world!";
string outStr = new String('-', oldString.Length);
Console.WriteLine(outStr);

      



Take a look at this working example

+6


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics