how to convert lower case character to upper case? - iphone

unichar c;
c = [myString characterAtIndex:0];
unichar catchcar = [c lowercaseString];
error : invalid reciever type unicar.
I know lowercaseString is used to covert String not character. Is there any solution?

you could do the following:
unichar catchcar = [[myString lowercaseString] characterAtIndex: 0];
If you have a a character only, do the following:
// given unichar c
unichar catchcar = [[[NSString stringWithCharacters:&c length:1] lowercaseString] characterAtIndex: 0];

The simplest solution for converting case on a single character is to just use the C functions tolower and toupper. Using the code example from the question and rewriting that would give this for conversion to lowercase:
#import <ctype.h>
unichar catchcar = tolower([myString characterAtIndex:0]);
No need to do anything complicated with the NSString API. And no need to make the whole string lowercase first either.
Hope this helps,
Erik

unichar c = [[myString lowercaseString] characterAtIndex:0];
Try this.

Related

NSString unichar from int

I have an int value which I obtained from the character 爸, which is 29240. I can convert this number to hex, but I have no clue how to write the chinese character out in an NSString with only the int 29240.
Basically, what I did was:
NSString * s = #"爸";
int a = [s characterAtIndex:0];
NSLog(#"%d", a);
What it gave as output was 29240.
However, I don't know how to create an NSString that just contains 爸 from only the int 29240.
I converted 29240 into binary which gave me 7238, but I can't seem to create a method which allows me to input any integer and NSLog the corresponding character.
I can hard code it in, so that I have
char cString[] = "\u7238";
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"result string: %#", string);
But I'm not sure how to do it with any int.
Thanks to anyone who can help me!
To create a string from one (or more) Unicode characters use initWithCharacters:
unichar c = 29240;
NSString *string = [[NSString alloc] initWithCharacters:&c length:1];
NSString uses UTF-16 characters internally, so
this works for all characters in the "Basic Multilingual Plane", i.e. all characters up to U+FFFF. The following code works for arbitrary characters:
uint32_t ch = 0x1F60E;
ch = OSSwapHostToLittleInt32(ch); // To make it byte-order safe
NSString *s1 = [[NSString alloc] initWithBytes:&ch length:4 encoding:NSUTF32LittleEndianStringEncoding];
NSLog(#"%#", s1);
// Output: 😎
Try out this code snippet to get you started in the right direction:
NSString *s = #"0123456789";
for (int i = 0; i < [s length]; i++) {
NSLog(#"Value: %d", [s characterAtIndex:i]);
}
Just pass in the character as an integer:
unichar decimal = 12298;
NSString *charStr = [NSString stringWithFormat:#"%C", decimal];

How to split the text of a UITextField into individual characters

I have a UITextField and suppose there are 5 characters in this textbox for e.g. hello.
Now I want to divide this word into single character i.e. 'h','e','l','l','o';
then how can I perform this kind of action.
Any help will be appreciated.
First get the string value from the textfield:
NSString *text = myTextField.text;
Then you can iterate each character:
NSUInteger len = [text length];
for (NSUInteger i = 0; i < len; i++)
{
unichar c = [text characterAtIndex:i];
// Do something with 'c'
}
Just use the NSString method UTF8String
const char *str = [textField.text UTF8String];
You then have an array of UTF8 chars
NOTE: ensure you are happy with the UTF8 encoding i.e. no non standard characters
I think using the for loop is a good idea, but to get exactly what you asked for, use -getCharacters:range:. Here is an example:
- (void)testGetCharsInRange
{
NSString *text = #"This is a test string, will it work?";
unichar *chars = calloc([text length], sizeof(unichar));
[text getCharacters:chars range:NSMakeRange(0, [text length])];
STAssertEquals(chars[[text length] - 1], (unichar)'?', nil);
free(chars);
}

Detecting if a character is either a letter or a number

In objective-c, how would I check if a single character was either a letter or a number? I would like to eliminate all other characters.
To eliminate non letters:
NSString *letters = #"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSCharacterSet *notLetters = [[NSCharacterSet characterSetWithCharactersInString:letters] invertedSet];
NSString *newString = [[string componentsSeparatedByCharactersInSet:notLetters] componentsJoinedByString:#""];
To check one character at a time:
for (int i = 0; i < [string length]; i++) {
unichar c = [string characterAtIndex:i];
if ([notLetters characterIsMember:c]) {
...
}
}
NSCharacterSet *validChars = [NSCharacterSet alphanumericCharacterSet];
NSCharacterSet *invalidChars = [validChars invertedSet];
NSString *targetString = [[NSString alloc] initWithString: #"..."];
NSArray *components = [targetString componentsSeparatedByCharactersInSet:invalidChars];
NSString *resultString = [components componentsJoinedByString:#""];
Many ways to do it, here is one using character sets:
unichar ch = '5';
BOOL isLetter = [[NSCharacterSet letterCharacterSet] characterIsMember: ch];
BOOL isDigit = [[NSCharacterSet decimalDigitCharacterSet] characterIsMember: ch];
NSLog(#"'%C' is a letter: %d or a digit %d", ch, isLetter, isDigit);
You can use the C functions declared in ctype.h (included by default with Foundation). Be careful with multibyte characters though. Check the man pages.
char c = 'a';
if (isdigit(c)) {
/* ... */
} else if (isalpha(c)) {
/* ... */
}
/* or */
if (isalnum(c))
/* ... */
Checking if a character is a (arabic) number:
NSString* text = [...];
unichar character = [text characterAtIndex:0];
BOOL isNumber = (character >= '0' && character <= '9');
It would be different if you would to know other numeric characters (indian numbers, japanese numbers etc.)
Whether character is a letter depends on what you mean by letter. ASCII letters? Unicode letters?
I'm not too familiar with obj-c, but this sounds like something that can be achieved using a regex pattern.
I did some searching and found this:
http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html
Try running a regex of "[0-9]" on each character to determine if it is a number.
Save the number to a temporary array, your array should end up with only the numbers.
I would type out the code...but as I said before, I'm not familiar with obj-c. I hope this helps though.
You can check them by comparing the ASCII value for number (0-9 ASCII ranged from 48-57) .. Also you can use NSScanner in Objective C to test for int or a char.

Finding element in array

I have the follow code:
NSArray *myArray = [NSArray arrayWithObjects: #"e", #"è", #"é",#"i","ò",nil];
NSString *string = #"simpleè";
NSMutablestring *newString;
for(i=0>;i< [string length]; i++){
if([stringa characterAtIndex:i] is in Array){
[newString appendFormat:#"%c", [string characterAtIndex:i]];
}
}
How make finding if single char of string stay in the array?
Example of result:
newString= #"ieè";
I think you want to apply rangeOfCharacterFromSet:options:range: repeatedly. You'll have to create a NSCharacterSet from the characters in your array somehow.
Added
Though it probably would be just as simple to just loop through the string with characterAtIndex and compare each char (in an inner loop) to the chars in your array (which you could extract into a unichar array or put into a single NSString to make easier to access).
Umm... if you want to check what the values are you can use NSLog
NSLog"%f", myFloat;
So you can use this to check your array... Which is what I think you are asking for, but the grammar in your question isn't very good. Please provide more details and better grammar.
You should check the length of your string and then match your string characters with the array and if found append that character in a new string.
NSString *mstr = #"asdf";
NSString *b = [ mstr characterAtIndex:0];
Hope it helps.......
You'll want to create an NSCharacterSet with the characters in the string and then ask each string in the array for its rangeOfCharacterFromSet:. If you find one where a range was actually found, then a character from the string is in the array. If not, then none of them are.
This might seem a bit roundabout, but Unicode makes looking at strings as just a series of "chars" rather unreliable, and you'll want to let your libraries do as much of the heavy lifting for you as they can.
There are better ways to do this, but this is what I think you're trying to do:
NSMutableString* result= [NSMutableString stringWithString:#""];
for( int i= 0; i < [string length]; ++i ) {
NSString* c= [NSString stringWithFormat:#"%C", [string characterAtIndex:i]];
if( [myArray containsObject:c] )
[result appendString:c];
}

Checking for multiple characters in nsstring

i have a string and i want to check for the multiple characters in this string the following code i working fine for one character how to check for the multiple characters.
NSString *yourString = #"ABCCDEDRFFED"; // For example
NSScanner *scanner = [NSScanner scannerWithString:yourString];
NSCharacterSet *charactersToCount = #"C" // For example
NSString *charactersFromString;
if (!([scanner scanCharactersFromSet:charactersToCount intoString:&charactersFromString])) {
// No characters found
NSLog(#"No characters found");
}
NSInteger characterCount = [charactersFromString length];
UPDATE: The previous example was broken, as NSScanner should not be used like that. Here's a much more straight-forward example:
NSString* string = #"ABCCDEDRFFED";
NSCharacterSet* characters = [NSCharacterSet characterSetWithCharactersInString:#"ABC"];
NSUInteger characterCount;
NSUInteger i;
for (i = 0; i < [yourString length]; i++) {
unichar character = [yourString characterAtIndex:i];
if ([characters characterIsMember:character]) characterCount++;
}
NSLog(#"Total characters = %d", characterCount);
Have a look at the following method in NSCharacterSet:
+ (id)characterSetWithCharactersInString:(NSString *)aString
You can create a character set with more than one character (hence the name character set), by using that class method to create your set. The parameter is a string, every character in that string will end up in the character set.
Also look up NSCountedSet. It can help you keep count of multiple instances of the same character.
For example, from the docs:
countForObject:
Returns the count associated with a given object in the receiver.
- (NSUInteger)countForObject:(id)anObject
Parameters
anObject
The object for which to return the count.
Return Value
The count associated with anObject in the receiver, which can be thought of as the number of occurrences of anObject present in the receiver.