How to make a "0000001" NSString - objective-c

Another noob question from me.
I'm trying to create an application that counts, and I'd like to display the number value as something like "000001". So when it counts up it'll display as "000002".
I'm using something like this right now, but it's not working:
counter.text = [NSString stringWithFormat:#"%000f", count];
The counter.text is a UILabel.
Thanks for the answer.

If you're storing the number as an integer, use the %0xd format (where x is the number of digits) like so:
counter.text = [NSString stringWithFormat:#"%06d", count];

try this
NSLog(#"%.6d",3);
more info please refer to NSDataformatter.

Related

Objective C - NSRange and rangeOfString

I have a little problem with NSRange and rangeOfString. I want to search a substring in a given string which is working fine, but only to find a exact string and theres the problem i need to find a substring which begins always the same and ends always the same. I tried it already with something like that:
match = [strIn rangeOfString: #"truni/begin/*/end"];
But thats not working. So i need a way to to do this. Here is the specific part of the Code in full:
NSRange match;
match = [strIn rangeOfString: #"turni/begin/sHjeUUej/end"];
NSRange range = NSMakeRange(match.location, match.length);
NSString *strOut = [strIN substringWithRange:range];
You see the string "turni/begin/sHjeUUej/end" will always be the same except for the part "sHjeUUej". Hope someone can help me.
Thanks in advance.
Use a regular expression with:
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask
with an option of RegularExpressionSearch.
See ICU User Guide Regular Expressions for information on creating regular expressions.
you can use prefix/suffix
if ([strIn hasPrefix:#"truni/begin/"] && [strIn hasSuffix:#"end"]) {
//match
You can use a simpler solution if you make sure that your string always starts with turni/begin/ and ends with /end.
You can use:
NSString *strOut = [[strIn stringByReplacingOccurrencesOfString:#"turni/begin/" withString:#""] stringByReplacingOccurrencesOfString:#"/end" withString:#""];
With that, you can retrieve the string between the two others with only one line of code and less comparations.

Using multiple images and to display at random

How could I make it randomize what image shows up so that there is a different one displaying every time. Here is my code, and the images I want to add to get randomized are for instance "tutorial2.png" and "tutorial3.png". Where would I add them into this line of code so that it randomizing which one is displayed. I'm not too experience either so detail and code would be appreciated, thanks.
self.help=[CCSprite spriteWithFile:#"tutorial.png"];
self.help.position=ccp(ws.width/2,ws.height*4/4/2);
[self addChild:self.help];
You first add images to Array eg: theArray.
then using following code to get random index
NSUInteger randomIndex = arc4random() % [theArray count];
then.
self.help=[CCSprite spriteWithFile:[NSString stringWithFormat:#"%#",[theArray[i]]]];
self.help.position=ccp(ws.width/2,ws.height*4/4/2);
[self addChild:self.help];

Replacing a single occurrence of string Obj-c

I am trying to replace only one of the occurrences of a NSString with another string. I know that you can use stringByReplacing*Occurrences*OfString; however, that replaces all occurrences. What I am looking for is more like stringByReplacing*Occurrence*OfString. If anyone could help me that would be great. Thanks,
Jnani
Something like this?
NSRange location = [someString rangeOfString:stringToReplace];
NSString* result = [someString stringByReplacingCharactersInRange:location withString:stringToReplaceWith];

parse strings separated by 2 elements

On Mac OS X, in Ojective-C, I wanted to know if anyone could give me some pointers as to how I could parse strings contained between "x" and "y".
My current code only enables me to separate the strings separated by one componennt :
NSArray * allLines = [theDataObject componentsSeparatedByString:#"word-1"];
Ideally, I would like to isolate the strings contained between #"word-1" and #"word-2".
Could anyone help please? Thanks in advance!
Check the documentation for NSScanner. The methods you want are -scanUpToString:intoString: and -scanString:intoString:
NSString *str = [theDataObject stringByReplacingOccurencesOfString:#"word-2" withString:#"word-1"];
NSArray *allLines = [str componetnsSeperatedByString:#"word-1"];
You don't care whether its word-1 or word-2 as those do not come back in the array anyways.

Transforming a NSString #"123" in an (short)array[] = {1, 2, 3}?

I can't imagine a "clean" or efficient method of doing this.
I would like to transform a string of numbers like #"1234" into an array of shorts for a calculator pet project*.
I think of getting substrings and then the intValue of the substring but it sounds cumbersome and overkill. Would there be a more elegant way of doing it?
Thanks in advance!
* I know that there are more efficient ways to do maths and plenty of C libraries do this but it's for my own education :-).
If I have this straight, you want to turn an NSString into a short array? This function assumes each character in the NSString is a separate short. Oh, and don't forget to free that array when you're done with it!
//Stephen Melvin <jinksys#gmail.com>
short *NSStringToShortArray(NSString *digits){
int count = [digits length];
short *shortArray = malloc(sizeof(short)*count);
for(int i = 0; i<count; i++){
shortArray[i] = (short)[digits characterAtIndex:i] - '0';
}
return shortArray;
}
A good calculator interface should take one string of characters as a single number then after the operation is selected it clears the field to accept the next number.
For example.
You type in 2 then hit the * button, the field clears and you type in 2 again and press = and get 4.
If you want the interface to accept an equation or a script then you should read up on parsing numeric equations.