capitalization

  • Thread starter Thread starter Aaron
  • Start date Start date
A

Aaron

How do you detect a string that is all capitalized?

ie.

string s = "MY NAME IS"; return true;
string s = "My Name is"; return false;
 
Aaron said:
How do you detect a string that is all capitalized?

ie.

string s = "MY NAME IS"; return true;
string s = "My Name is"; return false;

if ( s == s.ToUpper( ) )
{
// it's in all caps
}

Good luck,
Ryan LaNeve
MCSD.NET
 
Ryan said:
if ( s == s.ToUpper( ) )
{
// it's in all caps
}

That will also detect strings that don't contain any letters... if s is
"1234" it will return true. You may want to make sure the string
contains at least one letter.

Jesse
 
Jesse McGrew said:
That will also detect strings that don't contain any letters... if s is
"1234" it will return true. You may want to make sure the string
contains at least one letter.

Jesse

See...that's why I shouldn't try to "help" at 3 AM on New Year's Day!

Ryan
 
Hi,

Probably this might help you

bool rValue = false;
if (s.ToUpper() == s)
{
rValue = true;
}
return (rValue);

Regards

RK Scharma
 
Try a regular expression. Depending on your meaning
of all caps, the following may give you what you want:

Running the following:
-----------------------------------------------------------------
using System;
using System.Text.RegularExpressions;

class Test {
public static bool ValidateAllCaps(string inString)
{
Regex r = new Regex("^[A-Z ]+$"); // Note the space between Z & ]
return r.IsMatch(inString);
}

public static void Main(string[] args)
{
foreach (string s in args)
{
Console.WriteLine("The string '{0}' {1} all caps",
s, (Test.ValidateAllCaps(s)? "is": "is not"));
}
}
}
-----------------------------------------------------------------
gives:

D:\_csharp\regex>allcaps "MY NAME IS" "My Name is" 1234 "MY NAME IS 6"
The string 'MY NAME IS' is all caps
The string 'My Name is' is not all caps
The string '1234' is not all caps
The string 'MY NAME IS 6' is not all caps

There's lots of info about regular expressions on the internet. For example:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemtextregularexpressionsregexclasstopic.asp

Garrett
 
Back
Top