Get a char from NSString and convert to int - objective-c

In C# I can convert any char from my string to integer in the following manner
intS="123123";
int i = 3;
Convert.ToInt32( intS[i].ToString());
What is the shortest equivalent of this code in Objective-C ?
The shortest one line code I've seen is
[NSNumber numberWithChar:[intS characterAtIndex:(i)]]

Many interesting proposals, here.
This is what I believe yields the implementation closest to your original snippet:
NSString *string = #"123123";
NSUInteger i = 3;
NSString *singleCharSubstring = [string substringWithRange:NSMakeRange(i, 1)];
NSInteger result = [singleCharSubstring integerValue];
NSLog(#"Result: %ld", (long)result);
Naturally, there is more than one way to obtain what you are after.
However, As you notice yourself, Objective-C has its shortcomings. One of them is that it does not try to replicate C functionality, for the simple reason that Objective-C already is C. So maybe you'd be better off just doing what you want in plain C:
NSString *string = #"123123";
char *cstring = [string UTF8String];
int i = 3;
int result = cstring[i] - '0';
NSLog(#"Result: %d", result);

It doesn't explicitly have to be a char. Here is one way of doing it :)
NSString *test = #"12345";
NSString *number = [test substringToIndex:1];
int num = [number intValue];
NSLog(#"%d", num);

Just to provide a third option, you can use NSScanner for this too:
NSString *string = #"12345";
NSScanner *scanner = [NSScanner scannerWithString:string];
int result = 0;
if ([scanner scanInt:&result]) {
NSLog(#"String contains %i", result);
} else {
// Unable to scan an integer from the string
}

Related

Simple Objective C program - It can't be this complex

I'm in the process of learning Objective C and decided to create a simple command line program. The idea is that is asks you for your name and then displays it backwards, capitalizing the first letter of each word. I got it done but the solution seems overly complex. I can't help but feel there is a better way.
char word [256];
printf("What is your name: ");
scanf("%[^\n]s",word);
// Convert the char array to NSString
NSString * inputString = [[NSString alloc] initWithCString: word encoding: NSUTF8StringEncoding];
//This will be our output string
//NSString *nameReversed = [[NSString alloc] init]; //alloc, init are needed to create an instance of this object
NSString *nameReversed = #"";
// Make inputString all lower case
inputString = [inputString lowercaseString];
// Get length of inputString and type cast it as an int and decrement by one
int length = (int)([inputString length])-1;
BOOL foundSpace = NO;
for (int i = 0; i<=(length); i++) {
// Setup the range
NSRange range = NSMakeRange(length-i,1);
// Get the a char from the input string
NSString *inputChar = [inputString substringWithRange:range];
// If this is the first char then make it upper case
if (i==0) {
inputChar = [inputChar capitalizedString];
}
// See if the last char was a space and if so make this char upper case
if (foundSpace){
foundSpace = NO; // Reset foundSpace
// Set this char to upper case
inputChar = [inputChar capitalizedString];
}
// See if this char is a space. If so, we'll need to convert the next char to upper case
if ([inputChar isEqual: #" "]) {
foundSpace = YES;
}
// Add the char to nameReversed
nameReversed = [nameReversed stringByAppendingString:inputChar];
}
printf("%s \n", [nameReversed UTF8String]);
Any insight would be appreciated!
Your program doesn't handle composed character sequences properly.
Also, capitalizedString will capitalize the first letter of each word in the string. So you can just call it once.
static NSString *reversedString(NSString *string) {
NSMutableString *reversed = [NSMutableString stringWithCapacity:string.length];
[string enumerateSubstringsInRange:NSMakeRange(0, string.length)
options:NSStringEnumerationReverse
| NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[reversed appendString:substring];
}];
return reversed;
}
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSData *inputData = [[NSFileHandle fileHandleWithStandardInput] readDataToEndOfFile];
NSString *original = [[NSString alloc] initWithData:inputData encoding:NSUTF8StringEncoding];
NSString *reversed = reversedString(original);
NSString *reversedAndCapitalized = [reversed capitalizedString];
printf("%s\n", reversedAndCapitalized.UTF8String);
}
return 0;
}
In a real app I'd probably add a category on NSString defining a reversedString method, instead of making it a free function.
If you assume the input is in ascii encoding.
char word [256];
printf("What is your name: ");
scanf("%[^\n]s", word);
// reverse
for (NSInteger i=0,j=strlen(word)-1; i<j; i++,j--) {
char tmp = word[i];
word[i] = word[j];
word[j] = tmp;
}
NSString *str0 = [NSString stringWithCString:word encoding:NSASCIIStringEncoding];
NSLog(#"%#", [str0 capitalizedString]);

Extract a 8 digit number from string of variable size

I have a string of undetermined size which contains a 8 digit number. Example:
This is 123456 a string
This is a new string 123456
123456 This is another string
How can I extract the integer value of such number?
NSString has an instance method -integerValue which returns the integer value extracted from the string's contents. So [#"123456" integerValue] will return 123456.
I think what you want is an NSScanner. Have the scanner scan up to the first occurrence of the decimalDigitCharacterSet and then use scanInteger: to get the NSInteger value it finds.
You can use regular expressions to pull numbers of any given format out of a string. If you just wanted to grab one integer from any given string (in this case, the first integer):
NSString* str = #"This is 123456 a string";
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:#".*?(\\d+).*?" options:NULL error:nil];
NSArray* results = [regex matchesInString:str options:NULL range:NSMakeRange(0, [str length])];
NSString* str2 = [str substringWithRange:[(NSTextCheckingResult*)[results objectAtIndex:0] rangeAtIndex:1]];
NSLog(#"%#",str2);
NSInteger intResult = [str2 integerValue];
Extract a number of the given digits from a string of undeterminate length.
#import <Foundation/Foundation.h>
NSNumber* numberFromStringWithDigits(NSString* string, NSUInteger digits)
{
NSScanner *scanner = [NSScanner scannerWithString:string];
NSCharacterSet *allowed = [NSCharacterSet decimalDigitCharacterSet];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:allowed intoString:&buffer]) {
if ([buffer length]==digits){
return [NSNumber numberWithInt:[buffer integerValue]];
}
} else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
return nil;
}
int main(int argc, char *argv[]) {
#autoreleasepool {
NSString *url = #"http://garb23age.com/x555xx/xx12345678xxxxxx";
NSDate *startTime = [NSDate date];
NSNumber *n = numberFromStringWithDigits(url,8);
NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow];
NSLog(#"%# in %f seconds",n,-elapsedTime); // 12345678 in 0.000188 seconds
}
}
#Bri An C: probably what you want is to extract the parameterString of the NSURL. Maybe you should post the URL to ask for alternate solutions.

NSString with Emojis

I have a NSArray containing NSStrings with emoji codes in the following format:
0x1F463
How can I now convert them into a NSString with the correct format?
With this method I am able to generate an "Emoji"-NSString:
NSString *emoji = [NSString stringWithFormat:#"\U0001F463"];
But this is only possible with constant NSStrings. How can I convert the whole NSArray?
Not my best work, but it appears to work:
for (NSString *string in array)
{
#autoreleasepool {
NSScanner *scanner = [NSScanner scannerWithString:string];
unsigned int val = 0;
(void) [scanner scanHexInt:&val];
NSString *newString = [[NSString alloc] initWithBytes:&val length:sizeof(val) encoding:NSUTF32LittleEndianStringEncoding];
NSLog(#"%#", newString);
[newString release]; // don't use if you're using ARC
}
}
Using an array of four of your sample value, I get four pairs of bare feet.
You can do it like this:
NSString *str = #"0001F463";
// Convert the string representation to an integer
NSScanner *hexScan = [NSScanner scannerWithString:str];
unsigned int hexNum;
[hexScan scanHexInt:&hexNum];
// Make a 32-bit character from the int
UTF32Char inputChar = hexNum;
// Make a string from the character
NSString *res = [[NSString alloc] initWithBytes:&inputChar length:4 encoding:NSUTF32LittleEndianStringEncoding];
// Print the result
NSLog(#"%#", res);

XOR'ing two hex values stored as an NSString?

here is yet another silly question from me!
NSString *hex1 = #"50be4f3de4";
NSString *hex2 = #"30bf69a299";
/* some stuff like result = hex1^hex2; */
NSString *result = #"6001269f7d";
I have a hex value as a string, stored in two diff. variables. i need to Xor them and the result should be in another string variables?
i tried them by converting string --> NSData --> bytes array --> xor'ing them ...but i have no success.....
thank you in advance...
You have to convert every character to Base16(for hexadecimal) format first.Then you should proceed with XORing those characters.You can use the strtol() function to achieve this purpose.
NSString *hex1 = #"50be4f3de4";
NSString *hex2 = #"30bf69a299";
NSMutableArray *hexArray1 = [self splitStringIntoChars:hex1];
NSMutableArray *hexArray2 = [self splitStringIntoChars:hex2];
NSMutableString *str = [NSMutableString new];
for (int i=0; i<[hexArray1 count]; i++ )
{
/*Convert to base 16*/
int a=(unsigned char)strtol([[hexArray1 objectAtIndex:i] UTF8String], NULL, 16);
int b=(unsigned char)strtol([[hexArray2 objectAtIndex:i] UTF8String], NULL, 16);
char encrypted = a ^ b;
NSLog(#"%x",encrypted);
[str appendFormat:#"%x",encrypted];
}
NSLog(#"%#",str);
Utility method that i used to split characters of the string
-(NSMutableArray*)splitStringIntoChars:(NSString*)argStr{
NSMutableArray *characters = [[NSMutableArray alloc]
initWithCapacity:[argStr length]];
for (int i=0; i < [argStr length]; i++)
{
NSString *ichar = [NSString stringWithFormat:#"%c", [argStr characterAtIndex:i ]];
[characters addObject:ichar];
}
return characters;
}
Hope it helps!!

How to get the first N words from a NSString in Objective-C?

What's the simplest way, given a string:
NSString *str = #"Some really really long string is here and I just want the first 10 words, for example";
to result in an NSString with the first N (e.g., 10) words?
EDIT: I'd also like to make sure it doesn't fail if the str is shorter than N.
If the words are space-separated:
NSInteger nWords = 10;
NSRange wordRange = NSMakeRange(0, nWords);
NSArray *firstWords = [[str componentsSeparatedByString:#" "] subarrayWithRange:wordRange];
if you want to break on all whitespace:
NSCharacterSet *delimiterCharacterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSArray *firstWords = [[str componentsSeparatedByCharactersInSet:delimiterCharacterSet] subarrayWithRange:wordRange];
Then,
NSString *result = [firstWords componentsJoinedByString:#" "];
While Barry Wark's code works well for English, it is not the preferred way to detect word breaks. Many languages, such as Chinese and Japanese, do not separate words using spaces. And German, for example, has many compounds that are difficult to separate correctly.
What you want to use is CFStringTokenizer:
CFStringRef string; // Get string from somewhere
CFLocaleRef locale = CFLocaleCopyCurrent();
CFStringTokenizerRef tokenizer = CFStringTokenizerCreate(kCFAllocatorDefault, string, CFRangeMake(0, CFStringGetLength(string)), kCFStringTokenizerUnitWord, locale);
CFStringTokenizerTokenType tokenType = kCFStringTokenizerTokenNone;
unsigned tokensFound = 0, desiredTokens = 10; // or the desired number of tokens
while(kCFStringTokenizerTokenNone != (tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer)) && tokensFound < desiredTokens) {
CFRange tokenRange = CFStringTokenizerGetCurrentTokenRange(tokenizer);
CFStringRef tokenValue = CFStringCreateWithSubstring(kCFAllocatorDefault, string, tokenRange);
// Do something with the token
CFShow(tokenValue);
CFRelease(tokenValue);
++tokensFound;
}
// Clean up
CFRelease(tokenizer);
CFRelease(locale);
Based on Barry's answer, I wrote a function for the sake of this page (still giving him credit on SO)
+ (NSString*)firstWords:(NSString*)theStr howMany:(NSInteger)maxWords {
NSArray *theWords = [theStr componentsSeparatedByString:#" "];
if ([theWords count] < maxWords) {
maxWords = [theWords count];
}
NSRange wordRange = NSMakeRange(0, maxWords - 1);
NSArray *firstWords = [theWords subarrayWithRange:wordRange];
return [firstWords componentsJoinedByString:#" "];
}
Here's my solution, derived from the answers given here, for my own problem of removing the first word from a string...
NSMutableArray *words = [NSMutableArray arrayWithArray:[lowerString componentsSeparatedByString:#" "]];
[words removeObjectAtIndex:0];
return [words componentsJoinedByString:#" "];