如何用所需字符填充字符串

问题描述 投票:0回答:3

我需要获取带有 8 个“*”符号的字符串。我如何在 .NET 3.5 中做到这一点? .NET 4.0?

谢谢。

.net string .net-3.5 .net-4.0
3个回答
15
投票

尝试:

string str = new string('*', 8);

1
投票

我自己的快速解决方法:

"".PadLeft(Password.Length,'*')

:)

但我想这应该是更好的解决方案。

谢谢。


0
投票

这对我有用

    private static string WelcomeString = "wordsandstuff";
    private static Int32 padInt = WelcomeString.Length;
    public static void Main(string[] args)
    {
        Console.WriteLine(WelcomeString);
        Console.WriteLine("".PadLeft(padInt, '='));
        Console.ReadKey();
    }
© www.soinside.com 2019 - 2024. All rights reserved.