Input must be upper case(first letter) and lower case (others)

牧云@^-^@ 提交于 2019-12-11 06:49:06

问题


I need some help creating a program that requires input with first letter in UPPER case and all other in lower case.

I tried to write some code, but I can't seem to figure it out.

EDIT: I think some of u didnt understand the problem. I have to create a loop that requires from the user to enter the first number in upper and others in lower, if the requirements are not met, the user must input once again, until the first letter is upper and others are lower.

var novaDrzava = new Država ();
Console.Write ("Vnesite ime (prva začetnica naj bo velika, ostale male): ");

novaDrzava.Ime = Console.ReadLine ();

var drzava = novaDrzava.Ime;
var inicialka = drzava.Substring (0);
var ostale = drzava.Substring (1, drzava.Length - 1);

for (int i = 0; i <= malecrke.Length; i++) {
    if (inicialka.Contains (velikecrke[i])) {
        if (ostale.Contains (malecrke[i])) {
            break;
        } else {
            Console.WriteLine ("Ponovno vnesite ime");
            novaDrzava.Ime = Console.ReadLine ();
        }
    }
}

回答1:


You might want to look into regular expressions. Something like this:

string inputOk = "Thisisatest";
string inputNok1 = "ThisisaTest";
string inputNok2 = "thisisatest";
bool resultOk = Regex.IsMatch(inputOk, "^[A-Z]{1}[a-z]+$");
bool resultNok1 = Regex.IsMatch(inputNok1, "^[A-Z]{1}[a-z]+$");
bool resultNok2 = Regex.IsMatch(inputNok2, "^[A-Z]{1}[a-z]+$");`



回答2:


If I understand it correctly it should be not difficult: just take the first letter with

var firstletter = yourstring.Substring(0, 1);

everything else with

var everthingelse = yourstring.Substring(1);

firstletter = firstletter.ToUpper();
everthingelse = everthingelse.ToLower();



回答3:


static void Main(string[] args)
        {
            string inputValue = Console.ReadLine();

            bool isValid = true;
            foreach (char val in inputValue)
            {
                if (inputValue.First()==val && char.IsUpper(val))
                {
                  //do nothing.
                }
                else if(char.IsLower(val))
                {
                    // do nothing.
                }
                else
                {
                    isValid = false;
                    Console.WriteLine("Invalid input string");
                    Console.ReadLine();
                    break;
                }
            }

            if (isValid)
            {
                Console.WriteLine("Valid input string");
                Console.ReadLine();
            }

        }



回答4:


You are, probably, looking for Title Case where each word starts from capital letter (e.g. John Smith). If it's your case:

 // Normilize: turn modificators in diactritics (e.g. "Hašek")
 string drzava = Console.ReadLine().Normalize();

 if (string.Equals(drzava, CultureInfo.CurrentCulture.TextInfo.ToTitleCase(drzava))) {
   // Correct name in title case
 }

Or if we want just single name (e.g. John, but not John Smith)

 if (!string.IsNullOrEmpty(drzava) &&
      drzava.All(c => char.IsLetter(c)) &&
      string.Equals(drzava, CultureInfo.CurrentCulture.TextInfo.ToTitleCase(drzava))) {
   // Correct Name : Not empty, Letters only, Title Case
 } 

Finally, you can try regular expressions

using System.Text.RegularExpressions;

... 

//TODO: change "*" into "+" if you want at least one lowercase symbol 
if (Regex.IsMatch(drzava, @"^\p{Lu}\p{Ll}*$")) {
   // Correct Name : Starts from upper case contains zero or more lowercase
}



回答5:


private static string CapitaliseFirstLetter(string str)
    {
        var Ustr = string.Empty;
        if (!String.IsNullOrEmpty(str))
        {
            Ustr = char.ToUpper(str.First()) + str.Substring(1).ToLower();
        }

        return Ustr;
    }


来源:https://stackoverflow.com/questions/55416779/input-must-be-upper-casefirst-letter-and-lower-case-others

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!