Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

teek

macrumors member
Original poster
Feb 12, 2008
88
0
Norway
unichar c = 'c';

if(c is lowercase) make it uppercase;

or

make c uppercase regardless of current case.

How do I do that ?
 
unichar c = 'c';

if(c is lowercase) make it uppercase;

or

make c uppercase regardless of current case.

How do I do that ?

You should never try to manipulate individual Unicode code points. You don't say what the type "unichar" is, but it may not be a complete code point, and it is most likely not a complete character. Then there are characters that come in three modes: lower case, upper case, and title case - The Title Case Would Be Used In A Sentence Like This, AND UPPERCASE LIKE THIS.

Check out CFStringCapitalize, CFStringUppercase and CFStringLowercase or the NSString equivalents.
 
Ok

I will look at those but to clear things up:

typedef unsigned short unichar;

It is returned from NSString instance method:

-(unichar)characterAtIndex:(NSUInteger)index
 
I will look at those but to clear things up:

typedef unsigned short unichar;

It is returned from NSString instance method:

-(unichar)characterAtIndex:(NSUInteger)index

Well, NSStrings seem to use UTF-16, which means every Unicode code point (there are more than a million of them) is stored as one or two 16 bit values. So most likely you get a Unicode code point, but sometimes you only get half.

But open "Character Viewer" and look at codes 01f1, 01f2 and 01fe (DZ, Dz and dz). These three are single Unicode characters! And when a sentence starts with one of these, and the first letter in a sentence should be capitalised, you need the Dz and not the DZ. Edit my post, and try clicking between d and z: You can't.

The correct thing is to use the functions that are provided, because lots of things in Unicode are just not simple. Even converting a character to uppercase is not simple.
 
Check the NSCharacterSet documentation to find out how to check whether a character is upper or lower, or the three NSString "changing case" methods to change regardless.

NSCharacterSet
NSString
 
Okay I get your point, this is great feedback.
But what if I limit the conversion to apply only if the characters are within the character range a-z and A-Z. Then I guess it's pretty safe to convert between upper and lower case, or are there other issues that I don't know about?
 
No need to restrict anything if you don't bother rolling your own solution. If you're changing the whole string, just use:

NSString *s = @"some text with UPPER and lower case letters";
NSString *upper = [s upperCaseString];

Or:
BOOL isLowerCase = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:index]];

If you want to change only the character at index, you'll need to use NSMutableString and replaceCharactersInRange:withString:

Let the framework do the work for you.
 
Okay I get your point, this is great feedback.
But what if I limit the conversion to apply only if the characters are within the character range a-z and A-Z. Then I guess it's pretty safe to convert between upper and lower case, or are there other issues that I don't know about?

What if your code may be used by Владимир Юшенко? Other languages do have cases as well, and the NSString functions, if I understand correctly, will make the proper case changes in those situations. It is probably best to rely on NSString to handle these things for you and not try to do it manually.
 
Ok I won't dig any deeper into this and I'll just go with NSString/NSMutableString NSCharacterSet and the lot. Thank you so much for your replies :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.