TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# String IsUpper, IsLower

Test strings with IsUpper and IsLower to see if they are all uppercase or lowercase.
IsUpper, IsLower. Some strings contain only lowercase letters. These strings do not need to be lowercased. The .NET Framework does not currently provide string type IsLower and IsUpper methods. We add them with extension methods in a static class.StaticStrings
Example. To start, this program contains a static class of extension methods. The IsUpper and IsLower methods are public static parameterful methods written with the extension method syntax.

Main: The Program class introduces the Main entry point, where we test the IsUpper and IsLower extension methods on various string literals.

String Literal

Info: A string is defined as uppercase if it contains no lowercase letters. It is defined as lowercase if it contains no uppercase letters.

Note: This is not the same as a string containing only uppercase or only lowercase letters.

Tip: You could change the implementation of these methods to fit this requirement if necessary.

C# program that implements string IsUpper and IsLower using System; static class Extensions { public static bool IsUpper(this string value) { // Consider string to be uppercase if it has no lowercase letters. for (int i = 0; i < value.Length; i++) { if (char.IsLower(value[i])) { return false; } } return true; } public static bool IsLower(this string value) { // Consider string to be lowercase if it has no uppercase letters. for (int i = 0; i < value.Length; i++) { if (char.IsUpper(value[i])) { return false; } } return true; } } class Program { static void Main() { Console.WriteLine("test".IsLower()); // True Console.WriteLine("test".IsUpper()); Console.WriteLine("Test".IsLower()); Console.WriteLine("Test".IsUpper()); Console.WriteLine("TEST3".IsLower()); Console.WriteLine("TEST3".IsUpper()); // True } } Output True False False False False True
Syntax. This article provides an example of the extension method syntax in the C# language. In large projects, using a static class of extension methods is often simpler than trying to remember different static methods in different classes.Extension
Summary. We implemented the IsUpper and IsLower extension methods on the string type in the C# language. These methods perform a fast scanning of the source string, rather than requiring another allocation and conversion.

Review: The IsUpper and IsLower methods can provide a clearer method call syntax as well as better performance for this test.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf