【问题标题】:How to get the first 4 letters to be uppercase letter in the next Textbox?如何在下一个文本框中将前 4 个字母变为大写字母?
【发布时间】:2016-03-19 15:00:24
【问题描述】:

编程新手及其用户/密码生成器。我想在左侧文本框中的每个名称中包含前 4 个字母,结果应该是 4 个大写字母(i)生成相同的 4 个大写字母/密码

例如:

Adair Ewing
Zorro Irving

(Pusching generatbutton)
(Result)

ADAI0ASAI / dkfnwkef
ZOOR1ZOOR / woeknfwe

我的代码:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonGenerat_Click(object sender, EventArgs e)
        {
            char[] radbytning = new char[]{'\r', '\n'};
            string[] name = textBoxLeft.Text.Split(radbytning, StringSplitOptions.RemoveEmptyEntries);
            string result = "";
            for (int i = 0; i <name.Length; i++)
            {
                result += string.Format("{0}{1}{0} / {2}\r\n", name[i].Substring(0, 4), i, GeneratPassword());
            }

            textBoxRight.Text = result;

        }

        private static string GeneratPassword() 
        {
            string result = "";
            String tecken = "abcdefghijklmnopqrstuvxyzåäö1234567890ABCDEFGHIJKLMNOPQRSTUVXYZÅÄÖ";
            Random random = new Random();
            for(int i = 0; i< 8; i++)
            {
                result += tecken[random.Next(tecken.Length)].ToString();
                System.Threading.Thread.Sleep(10);
            }
            return result;
        }

          public string FirstLetterToUpper(string)
        {
            if (str == null)
                return null;

            if (str.Length > 1)
                return char.ToUpper(str[0]) + str.Substring(1);

            return str;
        }
    }
}

【问题讨论】:

标签: c# uppercase toupper


【解决方案1】:

这行代码会引发错误,因为您将传递的“字符串”没有附加变量public string FirstLetterToUpper(string)

但是,我认为你根本不需要这个函数,你甚至没有在代码中调用它。

我认为你只需要更改以下代码行以获得你想要的并删除我上面提到的Function:

result += string.Format("{0}{1}{0} / {2}\r\n", name[i].Substring(0, 4), i, GeneratPassword());

改为:

result += string.Format("{0}{1}{0} / {2}\r\n", name[i].Substring(0, 4).ToUpper(), i, GeneratPassword());

然后给出一个输出

ADAI0ADAI / CMldQQY7

ZORR0ZORR / e3sbdxJQ

我认为这就是您的意思,并且您在这部分问题中有多个拼写错误!

【讨论】:

    猜你喜欢
    • 2016-04-11
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多