c#-输入字符串或列出/删除字符串的最后一个字符

c#-输入字符串或列出/删除字符串的最后一个字符,c#,console,passwords,C#,Console,Passwords,基本上,我正在创建一个基本密码脚本 我目前遇到的问题是注册退格以删除字符。我有检测后退空间的基础,但它们实际上没有做任何事情,因为我目前使用输入作为字符串,字符串不能使用简单的修改来删除最后一个字符,只要我知道。 由此导致的第二个问题是,当执行退格时,不会从控制台中删除替换真实字符的“*”字符。有没有办法修改控制台显示以删除它 另一方面,如果有一种从字符串中删除单个字符的方法,这将有助于本程序和其他程序将来了解 因此,在这个场景中,是否理想地将输入的字符插入到列表中,以及是否实现了退格以删除最后

基本上,我正在创建一个基本密码脚本

我目前遇到的问题是注册退格以删除字符。我有检测后退空间的基础,但它们实际上没有做任何事情,因为我目前使用输入作为字符串,字符串不能使用简单的修改来删除最后一个字符,只要我知道。 由此导致的第二个问题是,当执行退格时,不会从控制台中删除替换真实字符的“*”字符。有没有办法修改控制台显示以删除它

另一方面,如果有一种从字符串中删除单个字符的方法,这将有助于本程序和其他程序将来了解

因此,在这个场景中,是否理想地将输入的字符插入到列表中,以及是否实现了退格以删除最后一个索引列表元素

代码如下:

using System;
using System.Collections.Generic;

namespace MobileApplication
{
    public class Password
    {
        public void run()
        {
            while (true)
            {
                String b = "\n---------\n";
                String Pass = "TS";
                String passin = "";
                int count = 1;
                int limit = 4;

                Console.Write($"{b}- Login -{b}\n(Case senitive)");
                Console.Write($"\nPassword > ");
                while ( count <= limit) {
                    var key = System.Console.ReadKey(true);

                    //if (key.Key != ConsoleKey.Enter) {              // the below can be substituted for this but then backspage shows up as *

                    if (key.Key == ConsoleKey.A || key.Key == ConsoleKey.B || key.Key == ConsoleKey.C || key.Key == ConsoleKey.D ||
                        key.Key == ConsoleKey.E || key.Key == ConsoleKey.F || key.Key == ConsoleKey.G || key.Key == ConsoleKey.H ||
                        key.Key == ConsoleKey.I || key.Key == ConsoleKey.J || key.Key == ConsoleKey.K || key.Key == ConsoleKey.L ||
                        key.Key == ConsoleKey.M || key.Key == ConsoleKey.N || key.Key == ConsoleKey.O || key.Key == ConsoleKey.P||
                        key.Key == ConsoleKey.Q || key.Key == ConsoleKey.R || key.Key == ConsoleKey.S || key.Key == ConsoleKey.T ||
                        key.Key == ConsoleKey.U || key.Key == ConsoleKey.V || key.Key == ConsoleKey.W || key.Key == ConsoleKey.X ||
                        key.Key == ConsoleKey.Y || key.Key == ConsoleKey.Z)
                    {

                        passin += key.KeyChar;
                        Console.Write("*");
                    }
                   //  else if (key.Key == ConsoleKey.Backspace)    // this is the problem
                    // {
                   //     String temppass = passin;
                  //      passin -= key.KeyChar;
                   //  }
                    else if (key.Key == ConsoleKey.Enter)
                    {
                        Console.WriteLine($"\n{passin} ENTER");
                        //break;
                        if (passin == Pass)
                        {
                            Console.WriteLine("success haxer");
                        }
                        else
                        {
                            passin = "";
                            Console.Write($"incorrect password ({count} / {limit})\n\nPassword > ");
                            count++;
                        }

                    }
                }
                Console.Write($"U Failed 2 haxing");
                Console.Write($"{b}\n- Goodbye -\n{b}");
            }
        }
    }
}

使用系统;
使用System.Collections.Generic;
命名空间移动应用程序
{
公共类密码
{
公开募捐
{
while(true)
{
字符串b=“\n-------------\n”;
字符串Pass=“TS”;
字符串passin=“”;
整数计数=1;
整数极限=4;
Write($“{b}-Login-{b}\n(Case senitive)”;
控制台。写入($“\nPassword>”);
while(count您需要从屏幕上删除“*”-
控制台。Write(“\b\b”)
应该处理这个问题。 然后从字符串中删除字符,您可以只使用从字符串中删除函数。 所以代码应该是这样的:

else if (key.Key == ConsoleKey.Backspace)    // this is the problem
{
    Console.Write("\b \b");
    passin = passin.Remove(passin.Length - 1, 1);
}