0

I want to check if my string contains all the upper case value or not for that I am using the below code but It does not work as expected for case 1 and only work for case2.

Case 1

string myString = "SWEET POTATO";
myString.All(char.IsUpper)

Case 2

string eval = "POTATO";  
eval.All(char.IsUpper)

It does not give me any error but I want in both the cases it will return true because in both the cases string value is in Upper case letter

BionicCode
  • 1
  • 4
  • 28
  • 44
user10753256
  • 141
  • 7

1 Answers1

0

you could convert your String to Upcase and check eval:

string myString = "FOO BAR"
if (myString.ToUpper() == myString) {
    // your handling
}
Jonas
  • 198
  • 1
  • 14
  • While answering the question it is bad idea! Please don't create garbage just to check if string is all caps. – Alexei Levenkov Jun 21 '19 at 06:09
  • @AlexeiLevenkov, I appreciate your comment, but can you please elaborate what is garbage here. – Prasad Telkikar Jun 21 '19 at 06:38
  • @PrasadTelkikar string created as result of `myString.ToUpper()` becomes no longer referenced after that line and had to be garbage collected later... this may be ok for one-time operation on small string but quickly becomes a problem if this type of coding is used across big/long running system. – Alexei Levenkov Jun 21 '19 at 07:09