What is the Objective-C equivalent of Java's parseInt()? - iphone

m anew comer to iPhone...before i have developed for Android..Could any one tell me what is the alternate code in objective C..Thanx in advance
Int projectNumber = Integer.parseInt(projectNumber.trim());

int intProjectNumber = [[projectNumber stringByTrimmingCharactersInSet:whitespaceCharacterSet] integerValue];
edit:
Just to explain a bit more..
If you have a NSString named projectNumber (ie. #" 4 "). You can make a new string with trimed whitespace infront of the string and after the string with
NSString *trimedProjectNumber = [projectNumber stringByTrimmingCharactersInSet:whitespaceCharacterSet];
as you can see this replaces the trim() function
trimedProjectNumber would now be #"4". If you want an integer representation of this string you do:
int intProjectNumber = [trimedProjectNumber integerValue];
this replaces the parseInt..
I dont know java but i think this is what youre code does? If not explain what the java code does..

Related

iPhone: Dynamic spaces in NSString

It may be a simple question, but i could't get the answer and needing your help!
I have a string like,
NSString *temp = #"Hello How are you?";
I have to provide spaces dynamically starting in this string by code. For ex: I need to dynamically add 5 spaces in this string in starting point. So, the output string will be like,
#" Hello how are you?"
My doubt is, how can i add spaces dynamically to a existing string? I need it to do this way only, not via any other way like string concatenation etc. due to my requirement.
So, please advise me how can i add spaces dynamically in starting point of the existing string.
Note: The spaces will vary every time, its not constant that i can provide 5 spaces only, it will vary.
Thank you!
An NSString is immutable, so you have to create a new string in any case.
The following code will create a front-padded string with padLength spaces:
int padLength = 10;
NSString* originalString = #"original";
NSString* leadingSpaces = [#"" stringByPaddingToLength:padLength];
NSString* resultString = [NSString stringWithFormat:#"%#%#", leadingSpaces, originalString];

String manipulation in objective-c

this is hard to describe but I am currently catching a string from a database, this string can be 1-4 characters long, however I am wanting to always display 4 characters, so if i get say a string back that is 34, i want it to be 0034.
I have set up a method to catch the string so now I just need to figure out how to do this. what I then plan to do is feed that string into a NSArray so I can send each [i'th] of the array off to 4 differetn methods that control animations in my app.
The reason its in string format is because I have had to bounce it round from hex, to int to string for various formatting reasons within the application.
this is my code i have so far. Suggestions/solutions would be great thankyou, I am so new its hard to find solutions for stuff like string manipulation etc..
//... other method I am getting the string from/.
[self formatMyNumber:dataString];
///..
-(void)formatMyNumber:(NSString *)numberString{
//resultLabel.text = numberString; //check to make sure string makes it to here.
//NSLog(#"hello From formatMyNumber method"); //check
}
//..
//the with send off each character to 4 animation methods that accept integers.
- (void)playAnimationToNumber:(int)number{
//...
//UpDated... weird stuff happening.
here is my method so far.
//Number Formatter
-(void)formatMyNumber:(NSString *)numberString{
NSLog(#"This is what is passed into the method%#",numberString);
int tempInt = (int)numberString;
NSLog(#"This is after I cast the string to an int %i",tempInt);
//[NSString alloc] stringWithFormat:#"%04d", numberString];
NSString *tempString = [[NSString alloc] initWithFormat:#"%04d", tempInt];
NSLog(#"This is after I try to put zeros infront %#",tempString);
//resultLabel.text = tempString;
//NSLog(#"hello From formatMyNumber method");
}
this is the output.
[Session started at 2011-06-19
16:18:45 +1200.] 2011-06-19
16:18:54.615 nissanCode0.1[4298:207]
731 2011-06-19 16:18:54.616
nissanCode0.1[4298:207] 79043536
2011-06-19 16:18:54.617
nissanCode0.1[4298:207] 79043536
2011-06-19 16:18:54.617
nissanCode0.1[4298:207] hello From
formatMyNumber method
As far as the number of zeros preceding your string goes there are a couple of ways to do this. I'd suggest:
NSString *myString = [NSString stringWithFormat:#"%04d",[dataString intValue]];
Is it possible you could have the number in integer form instead of string form? If so, it's pretty easy to use [NSString stringWithFormat:#"%04d", number]. See here for a list of the possible format specifiers.
See what stringWithFormat: can do. I realize you mentioned your numbers are NSStrings, but if they were ints, or you convert them back to ints, the following may do the trick. Modify the following to best suit your need:
return [NSString stringWithFormat:#"%04d", number];

Create NSString with 'N' Unicode Characters

I'm trying to create a string containing (unicode) 'stars' based on an integer rating. I currently have:
NSMutableString *stars = [NSMutabelString stars];
for (int i = 0; i < rating; i++)
{
[stars appendString:#"\u2605"];
}
However, I find this a bit ugly. Does a way exist to construct such a string without using this looping method? Something using the string formats?
Sure - to do this on a single line you can use the stringByPaddingToLength method:
[#"" stringByPaddingToLength: rating withString: #"\u2605" startingAtIndex:0];
...should hopefully do the trick for you - and no need to create any subclasses or categories, etc!
You can make a category for NSString with an extra method - say, +(NSString)stringForRating:(NSInteger)rating, and move the loop in there. Then whenever you need a star string, just call that.

int to NSString issue

I use the following statement for into to NSString conversion (with a find/replace)
curr_rep_date = [tmpRptDt stringByReplacingOccurrencesOfString:tmpYrVal withString:[NSString stringWithFormat:(tmpCurrYearInt-1)]];
I have declared
int tmpYrVal;
NSMutableString *tmp_dt,*curr_rep_date;
But the program seems to be crashing and the debugger is not giving any hint.
Could someone help me with the issue and what would be the correct usage.
There's a number of problems here.
Firstly, sringByReplacingOcurrancesOfString:withString: is expecting NSStrings as parameters, not ints. That's the reason why it crashes. The method Is attempting to send a message to a primitive type, not an object.
Secondly, you need to use a proper format string for the stringWithFormat: method. This is the same as how NSLog works.
A format string can look like #"some text %d". It would then be followed by a comma separated list of values to be used in place of the % placeholders.
Example:
[NSString stringWithFormat:#"%d", myIntValue];
Will effectively turn your int into a string, as it creates a new string with a format using your int.
You invoked the stringWithFormat - Method without a format string. [NSString stringWithFormat: #"%i", (tmpCurrYearInt-1)] should solve your problem.
You are missing the format
curr_rep_date = [tmpRptDt stringByReplacingOccurrencesOfString:tmpYrVal withString:[NSString stringWithFormat:#"%d", (tmpCurrYearInt-1)]];
Basic int to NSString conversion works like this:
NSString* s = [NSString stringWithFormat:#"%d", intNumber];

Finding a string in a string

Does anyone know a nice efficient way of finding a string within a string (if it exists) in objective c for iPhone Development, I need to find the part of the string in between two words, e.g. here I need to find the co2 rating number in the string, where z is the value I'm looking for ...
xxxxxco_2zendxxxxxxx
Ideally, I'd use a regular expression for this, probably something like co_2(.*?)end, so I'd take a look at RegexKitLite as stimms suggests.
If that is not suitable, you could extract the string you're looking for with something like this:
NSString* src = #"xxxxxco_2zendxxxxxxx";
NSRange startMarker = [src rangeOfString:#"co_2"];
if (startMarker.location != NSNotFound) {
NSScanner* scanner = [NSScanner scannerWithString:src];
[scanner setScanLocation:startMarker.location + startMarker.length];
NSString* co2Value = #"";
[scanner scanUpToString:#"end" intoString:&co2Value];
NSLog(#"co_2 value is %#", co2Value);
} else {
NSLog(#"co_2 marker not found");
}
Here we look for #"co_2", failing if it's not found, then use an NSScanner to grab everything from just after that string to the next occurrence of #"end". Note that if #"end" is missing this code will silently grab the rest of the string.
This might be of interest to you (in particular the rangeOfString function):
(NSRange)rangeOfString:(NSString *)aString
Unfortunately Cocoa doesn't have any built-in RegEx support..
String matching is a well explored domain especially for algorithms dealing with genetic material. You could check out the Art of Computer programming for 10x more than you ever wanted to know about string matching.
Most of that is overkill and you would be fine using a regular expression. Check out http://regexkit.sourceforge.net/RegexKitLite/ a regex library which runs on the iphone.