3 Replies - 1290 Views - Last Post: 11 May 2012 - 07:30 AM

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Check is a string is uppercase w\Regular Expressions

Posted 30 December 2007 - 07:52 PM

Description: Pass the method a string, read the results (true/false). Need reference to System.Text.RegularExpressions NamespaceThis is a snippet used to check if a provided string is all uppercase or not.

//Namespace reference
using System.Text.RegularExpressions;

/// <summary>
/// method to check and see if a string is all uppercase
/// </summary>
/// <param name="str">string to check</param>
/// <returns></returns>
public bool IsUppercase(string str)
{
    //variable to hold our return value
    bool upper;
    //variable to hold our search pattern
    string pattern = "[a-z]";
    try
    {
        Regex AllCaps = new Regex(pattern);
        if (AllCaps.IsMatch(str))
        {
            upper = false;
        }
            upper = true;
    }
    catch
    {
        upper = false;
    }
    return upper;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Check is a string is uppercase w\Regular Expressions

#2 rasty_ytsar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 30-March 09

Re: Check is a string is uppercase w\Regular Expressions

Posted 30 March 2009 - 04:56 PM

THIS IS VERY HELPFUL FOR MY PROJECT MAKING A SIMPLE INTERPRETER
Was This Post Helpful? 0
  • +
  • -

#3 mucahid uslu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-February 08

Re: Check is a string is uppercase w\Regular Expressions

Posted 05 July 2011 - 12:21 AM

There is a simple bug that after if clause, upper variable set to true ! Fixed code below, bool upper; //variable to hold our search pattern string pattern = "[a-z]"; try { Regex AllCaps = new Regex(pattern); if (AllCaps.IsMatch(str)) { upper = false; } else { upper = true; } } catch { upper = false; } return upper;
Was This Post Helpful? 0
  • +
  • -

#4 raoz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-May 12

Re: Check is a string is uppercase w\Regular Expressions

Posted 11 May 2012 - 07:30 AM

An easier way - [code] if(str == str.ToUpper()) { return true; } else { return false; }
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1