Determine if first char of string is upper or lower

c# find uppercase characters in string
isupper c#
.net first char to upper
c check if character is lowercase
get first character of string c#
checking uppercase in c#
c# check character is uppercase
c# check if uppercase

I'm trying to determine whether the first char of the "sentence" string is capital or not. The program runs fine, but test run gives an array index out of range error. I can't understand the problem.

public class CapitalOrNot
{
    public static void Main()
    {
        string sentence = "Asdafasda";
        string UpOrLow = UpperOrLower(sentence);
        Console.WriteLine("First char is " + UpOrLow);
    }

    public static string UpperOrLower(string mj)
    {
        if (char.IsUpper(mj[0]))
        {
            mj = "upper";
        }
        else mj = "lower";

        return mj;
    }
}

You should check if the string is empty:

 public static string UpperOrLower(string mj)
 {
     if (string.IsNullOrEmpty(mj))
         return "bad input";

     return char.IsUpper(mj[0]) ? "upper" : "lower";
 }

In Java, how to find if first character in a string is upper case without , The code above would give the incorrect answer for a lower case toUpperCase​())) { System.out.println("First Letter Upper case match found :" + m.group()); } } } Don't forget to check whether the string is empty or null . If we  char character = characters.charAt (i); // checking for each character that its a Upper-case or Lower-case. // letter or not by calling Character class isUpperCase () method, // and isLowerCase () by passing the character. It returns true. // if the character passed is lower-case or upper-case and false. // if not separately.

Solution using null conditional operator:

public static string UpperOrLower(string str)
{
    return (str?.Any()).GetValueOrDefault() ? (Char.IsUpper(str.First()) ? "upper" : "lower") : "bad input";
}

C#, IsUpper(String, Int32) Method. This method is used to check whether the specified string at specified position matches with any uppercase letter or not. If it matches then it returns True otherwise returns False. The Char.IsLower method will return true if the specified character is a lower case character. The Char.IsUpper method will return true if the specified character is a upper case character.

How about this?

public class CapitalOrNot
{
    public static void Main()
    {
        string sentence = "Asdafasda";

        if(sentence.Length > 0 && sentence != null)
        {
            string UpOrLow = UpperOrLower(sentence);
            Console.WriteLine("First char is " + UpOrLow);
        }
        else
        {
           Console.WriteLine("You did not input a sentence");
        }
    }

    public static string UpperOrLower(string mj)
    {
        if (char.IsUpper(mj[0]))
        {
            mj = "upper";
        }
        else mj = "lower";

        return mj;
    }
}

Check whether the given character is in upper case, lower case or , Check whether the given character is in upper case, lower case or non Recommended: Please try your approach on {IDE} first, before moving on to the solution. Case conversion (Lower to Upper and Vice Versa) of a string using BitWise  Note: In the first if, the first char is lower so only that test will be executed. In the second if, the third char is upper. In the second if, the third char is upper. And: For non-letter chars such as digits and punctuation, IsLower and IsUpper will return false.

The problem happens when you calls the UpperOrLower without any value, it raises an exception, to solve that you could do something like the code below.

    public static string UpperOrLower(string mj)
    {
        if (string.IsNullOrEmpty(mj))
        {
            if (char.IsUpper(mj[0]))
            {
                mj = "upper";
            }
            else mj = "lower";
        }
        else
        {
            mj = "empty";
        }
        return mj;
    }

Regards.

Strings · The Julia Language, Char is a 32-bit AbstractChar type that is the default representation of characters in Julia. Char is the Determine whether the first argument is a substring of the second. If needle is See also: lowercasefirst , uppercase , lowercase , titlecase. String.capitalize() returns a copy of this string having its first letter upper-cased. String.capitalize() returns the original string, if it’s empty or already starts with an upper-case letter. To capitalize first character of each word in the given sentence, refer the second example provided below.

How to check if a character is uppercase in Python, Checking if a character is uppercase evaluates to True if the character is uppercase and False if the character is lowercase. Use str.isupper() to check if a character  In C#, Char.IsUpper() is a System.Char struct method which is used to check whether a Unicode character can be categorized as an uppercase letter or not. Valid uppercase letters will be the members of the UnicodeCategory: UppercaseLetter. This method can be overloaded by passing different type and number of arguments to it. Char.IsUpper(Char) Method

Tcl Built-In Commands - string manual page, If found, return the index of the first character in the first such match within nocase? pattern string: See if pattern matches string; return 1 if it does, 0 if it to string except that all upper (or title) case letters have been converted to lower case. The result is the string without first letter. Then i will add this string to first char that is converted to lower case – fedotoves Aug 25 '10 at 11:34

lcfirst - Manual, Returns a string with the first character of str lowercased if that character is alphabetic. Note that 'alphabetic' is determined by the current locale. ucfirst() - Make a string's first character uppercase; strtolower() - Make a string lowercase  Stack Overflow Public Some languages e.g. chinese don't differentiate between upper and lower case as How do I make the first letter of a string uppercase in

Comments
  • Works for me: ideone.com/Pft0pL and that was a straight copy paste
  • Could your test be feeding you an empty string? This would cause mj[0] to throw that error.
  • The code works fine; csharppad.com/gist/34ac6b62a620acefb634
  • doogle, I think that is the case. My teacher said that I shouldn't do it this way, but didn't give any input what I should do. He said I should process the empty string some other way.
  • What if the string starts with a space? IsLower = false, IsUpper = false...
  • To me, string.Any() is a really strange way of not using string.IsNullOrEmpty(). Does it have any benefits?
  • I wouldn't say there's a benefit. I agree, IsNullOrEmpty gets the point across a bit better, and adds the null check.
  • This did the trick. I did think about IsNullOrEmpty, but didn't know how to take advantage of it. Thanks a lot!!
  • Again this doesn't work if the first char is not a letter IE a space
  • But adding a char.IsLetter and a test for IsNullOrWhiteSpace could be simple to add and to explain and, if you allow me to say so, it will be a lot better.
  • nice... why use a local variable, when you can just return the results that you want.
  • I can't alter the main program, as this is an online program on my university website. I just have to code the subroutine.
  • Shouldn't sentence.Length > 0 && sentence != null replaced with string.IsNullOrEmpty(sentence)?
  • Yeah, that's better.... I would use string.IsNullOrEmpty(sentence) since it essentially does the same thing but nicer.