Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

best way to check whether a character is uppercase?

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the best way to check whether a string is uppercase?

I need to convert this string "thisMethodNeedsToBeParsed"
into "this method needs to be parsed"

can anyone think of the fastest / best way to do it?

thanks in advance!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Offhand, I can't think of anything clever. But you can always go char by char...

[ August 08, 2005: Message edited by: marc weber ]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another way is to see if the character is upper case by comparing its ascii value.

Top of my head some rough algo..............
--------------------------------------------
for(var i=0;i till length of string;i++)
var char = charOfString(i);
var asciiVal = ASCII(char);
var resultinString;
if(asciiVal between 65 to 90)
add "" and char to resultinString
else
add char to resultingString
for end
---------------------------------------------
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if(asciiVal between 65 to 90)



Of course, this fails when using non-ascii character sets. There is a way that uses the same concept. Given that most character sets have the upper case letters consecutively, you can just ask

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Timmy Marks:
Of course, this fails when using non-ascii character sets.



True.

There is a way that uses the same concept. Given that most character sets have the upper case letters consecutively, you can just ask



Well, of course we know that Java always uses unicode, which has a lot more uppercase characters then just A-Z. So in Java, your solution fails, too.

Anyway, why reinvent the wheel if Sun already did all the work for you?
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What is the best way to check whether a string is uppercase?



For this part the Simplest might be "if (str.equals(str.toUpperCase())) ..."
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

We can't know what the best way is without a lot more context, but this way seems to require the least typing.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

We can't know what the best way is without a lot more context, but this way seems to require the least typing.[/qb]<hr></blockquote>

Or even better (because it also applies to uppercase letters outside the A-Z block):



(I'm not sure that "$0" actually works - JavaDoc only seems to mention "\0"...)

[sorry for the german that somehow slipped in...]
[ August 10, 2005: Message edited by: Ilja Preuss ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alan Moore:
... " $0" ...


Very nice! That's the "clever" approach I suspected was possible, but I'm not up enough on regex to come up with that myself.

I've been looking at the Pattern class API, but I can't figure out how this works. How does $0 (or \\0) translate to the matched character?
[ August 09, 2005: Message edited by: marc weber ]
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When used in the replacement string, "$0" is replaced by whatever was matched by the regex. In this case, the regex only matches one character, so that's what gets inserted. This mechanism is described in the javadoc for the appendReplacement() method of the Matcher class:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Alan!

I didn't realize that String's replaceAll(String regex, String replacement) method could "pass" information from the regex matcher to the replacement String. Very interesting...
reply
    Bookmark Topic Watch Topic
  • New Topic