How to get a substring from NSSTring from index 0 to 99. i.e. first 100 characters
[myString substringToIndex:100]
You need to be sure that index 100 is valid, i.e. length of string is at least 100. Otherwise an exception will be thrown.
[str substringToIndex: 100]
Related
My dictionary looks like this
#{#"Blue": #"Big",
#"Red": #"medium",
#"Yellow": #"small"}
I would like to know that the highest key length is 6, because Yellow is the longest key
You can try this. Suppose a is your dictionary. You can find the source here: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html
NSArray *array = a.allKeys;
NSNumber* maxLength= [array valueForKeyPath:#"#max.length"];
NSLog(#"Longest is %lu",maxLength.integerValue);
What is the meaning of {2,4} when validating an email with the following regular expression:
NSString *emailRegEx = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegEx];
It mean words length is minimum 2 and maximum 4 and it takes Capital A to Z and small a to z characters..
{2,4} means the string has minimum 2 characters and maximum 4 characters (The length of string should be greater than or equal to 2 and less than or equal to 4).
For example: In email ids and after dot, .com,.in, .uk so on...
In above regular expression, {2,4} means you can take occurrence of [A-Za-z] between 2 to 4 inclusive.
Ref
http://www.regular-expressions.info/reference.html
http://www.regextester.com/
I want to create a siple app which swaps the bytes of 2 and 4 byte hex codes.
So it should do: from 1234 to 3421 swap. I google and found out that I have to use byteorder and CFSwapInt32 and CFSwapInt16.
Here is what I already got:
NSString *byteOrder = [NSString stringWithFormat:#"%d",CFSwapInt32(12345678)];
NSLog(byteOrder);
But instead of the correct swapped bytes I get: 1315027968 as the number of the NSLog.
Can someone help me or tell me what I did wrong? :) I just want to swap bytes so they are in reversed order
1234 -->3412
12 34 -->34 12
12345678 -->78563412
12 34 56 78 --> 78 56 34 12
Thank you
Try
NSString *byteOrder = [NSString stringWithFormat:#"%x",CFSwapInt32(0x12345678)];
%x will output a value as hexadecimal.
Starting a number with 0x will interpret it as a hexadecimal value.
Your original number is 12345678 which, in hex, is 0x00BC614E
The output you get in the log is 1315027968 which, in hex, is 0x4E61BC00
So everything is working correctly.
You can try doing the same in hex if you prefer:
NSString *byteOrder = [NSString stringWithFormat:#"%x",CFSwapInt32(0x00BC614E)];
NSLog(byteOrder);
should log 0x4E61BC00 while
NSString *byteOrder = [NSString stringWithFormat:#"%x",CFSwapInt32(0x12345678)];
NSLog(byteOrder);
should log 0x78563412
How can i format my NSString to be something like
1 Item1 7.00 0% 7.00
1 Item2 6.00 0% 6.00
i'm getting the string from html it is in table format. Any ideas?
You can use IEEE printf flags to specify field widths (it's in the iOS library documentation). That means that if you have the content of the table cells as separate values or objects, you can create your fixed-space string using NSString's stringWithFormat: method.
I think, it solves your problem?
NSString *str = [NSString stringWithFormat:#"%i \t %# \t %.2f %%%i %.2f",1,#"Item1",7.00,0,6.00];
NSLog(#"%#", str);
I have 2 NSArray's that are holding values...
For example NSArray 1 has values 1 2 4 in it
and NSArray 2 has values 1 2 4 5 6 in it.
How can I write code to compare these 2 arrays to get the following information...
Count the values that are the same (so in this case 3) and count the values that are not the same (in this case 2).
I am simply populating the arrays like this:
NSString *s = #"1,2,4";
NSArray *numbers = [s componentsSeparatedByString:#","];
where *s is actually getting the text from a UITextField. If sorting mattering in comparing can you show me code to sort to make sure the user doesnt put the numbers in order?
If you are fine with sets instead of arrays, you can use NSMutableSet instead of NSArray. NSMutableSet has nice methods like intersectSet: and minusSet:
I would probably use the following method of the NSArray class:
enumerateObjectsUsingBlock.
and code the block testing for membership in the other array with the method:
indexOfObjectIdenticalTo.
If this isn't clear to you let me know.