How to take the first character of a String and make it uppercase by storing it in a variable?

Asked

Viewed 165 times

2

        String user,newUser,newPassword, password, resposta, user1,pass1;
        user = "admin";
        password = "123";
        Console.WriteLine("LOGIN\n");

    body:
        Console.WriteLine("Ja possui login?");
        resposta = Console.ReadLine();

        /*
        gostaria de fazer com que a resposta acima lida pelo programa seja convertida para um unico caractere maiusculo para quando fizer a comparação não precisar colocar varias || dentro da condição.
        */

        if (resposta.Equals("N"))
        {
            Console.Write("Digite novo usuario: \n");
            newUser = Console.ReadLine();

            Console.Write("Digite nova senha: \n");
            newPassword = Console.ReadLine();

            Console.WriteLine("Criado com sucesso!\nAgora faça Login!");

            goto body;

        }
        else if (resposta.Equals("S"))
        {
            Console.Write("Digite usuario: \n");
            user1 = Console.ReadLine();

            if (user1.Equals(user))
            {
                Console.Write("Digite a senha: \n");
                pass1 = Console.ReadLine();

                if (pass1.Equals(password))
                {
                    Console.WriteLine("Logado com Sucesso!");
                }
                else
                {
                    Console.WriteLine("Senha incorreta, tente novamente:");
                    goto body;
                }
            }
            else
            {
                Console.WriteLine("Usuario incorreto, tente novamente!");
                goto body;
            }
        }

        Console.Read();

2 answers

6


var c = char.ToUpper(texto[0]);

I put in the Github for future reference.

Documentation.

But before using this, delete all your code and start again, this time write a code without goto and with the resources of C#. There are other mistakes, but these are urgent. I suggest learning by other means.

  • 2

    +1, By the way, at the time of learning is the best time for this, because later becomes addiction write code "tied with wire". Practically all of us end up "tying with wire" at one time or another in less ideal conditions, even if against the will, but can not be "life principle" if the intention is to program of truth.

  • Thanks @Maniero

  • I’m gonna rewrite everything, and I’m not gonna use it. This was a simple solution just for learning, I’m learning at university and this was just a test.

  • Learn to do it the right way, once you do it the wrong way you start to form a wrong idea in your head and it gets harder and harder to do it right. And see the best way to say thank you on the [tour].

  • @Maniero a lot of difference in performance compared to StartsWith()?

0

Greetings, I suggest you follow @Maniero’s recommendations.

In answer to your question, and just to your knowledge, follow the example:

Change this:

body:
   Console.WriteLine("Ja possui login?");
   resposta = Console.ReadLine();

For this:

body:
   Console.WriteLine("Ja possui login?");
   resposta = Console.ReadLine().ToUpper();// basta inserir o ToUpper" Tornar Maiúscula"
  • Thanks @Agnaldo I even thought of this solution. But I don’t know why it wasn’t working out. I’ll try again. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.