int to NSString issue - iphone

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];

Related

remove specific characters from NSString

I wants to remove specific characters or group substring from NSString.
mean
NSString *str = #" hello I am #39;doing Parsing So $#39;I get many symbols in &my response";
I wants remove #39; and $#39; and & (Mostly these three strings comes in response)
output should be : hello I am doing Parsing So i get many symbols in my response
Side Question : I can't write & #39; without space here, because it converted in ' <-- this symbol. so i use $ in place of & in my question.
you should use [str stringByReplacingOccurrencesOfString:#"#39" withString:#""]
or you need replace strings of concrete format like "#number"?
try below code ,i think you got whatever you want simply change the charecterset,
NSString *string = #"hello I am #39;doing Parsing So $#39;I get many symbols in &my response";
NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:#"#39;$&"];
NSString *result = [[string componentsSeparatedByCharactersInSet:trim] componentsJoinedByString:#""];
NSLog(#"%#", result);

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];

Objective - C Rookie NSString Problem

I have this code:
// Fill out the email body text
NSString *emailBody = (#"Name:%#\nNumber of People:\nDate:", name.text);
NSLog(#"%#", emailBody);
As you can see I'm trying to append name.text to the e-mail body just after "Name:". However, NSLog only outputs the string contained in name.text and none of the rest of the e-mail body. What am I doing wrong here that the code deletes the rest of the string apart from name.text?
E.G if name.text contained the text "Jack", then NSLog would only output "Jack" and not:
Name: Jack
Number of People: x
Date: x
Which is what I am looking for.
Can anyone give me an insight as to what I'm doing wrong?
Thanks,
Jack
Use +stringWithFormat method:
NSString *emailBody = [NSString stringWithFormat:#"Name:%#\nNumber of People:\nDate:", name.text];
What you have now is a valid code, but it doesn't do what you want:
(#"Name:%#\nNumber of People:\nDate:", name.text);
calls a comma operator - it evaluates its 1st parameter, discards it and returns 2nd parameter, so that's why emailBody is eventually filled with name.text value
You should write
NSString *emailBody = [#"Name:%#\nNumber of People:\nDate:" stringByAppendingString:name.text];
Or, if it doesn't compile,
[[NSString stringWithString:#"Name:%#\nNumber of People:\nDate:"] stringByAppendingString:name.text]
generally you want to either use stringWithFormat as was suggested, which creates an autorelease string that follows the format you have, or you can use initWithFormat instead, which creates a string you can manually manage for better memory behavior, if necessary.
some books will insist that for the iphone, which has limited memory, you don't depend on autorelease objects more than it absolutely necessary, so you'd often find this instead:
NSString *emailBody = [[NSString alloc] initWithFormat:#"Name:%#\nNumber of People:\nDate:", name.text];
Then you can use "emailBody" and immediately after you are done with it, put in this line:
[emailBody release];
This is a good habit to get into in general.

Encode and Decode using UTF-8 in iPhone

I'm looking for an example demonstrating how I can encode and then decode the same string using UTF-8. Encode and then Decode means I want to implement the methods in 2 areas where one can encode it and another is able to decode it.
I have seen the API but I didn't get much success:
stringWithCString:encoding:
stringWithUTF8String:
stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;
EDITED
I have the string øæ-test-2.txt which I am encoding as follows:
char *s = "øæ-test-2.txt";
NSString *enc = [NSString stringWithCString:s encoding:NSASCIIStringEncoding];
but am getting øæ-test-2.txt as output.
Now I want to get back the original string back i.e. øæ-test-2.txt
EDITED
I am getting øæ-test-2.txt from server and I need øæ-test-2.txt by decoding it. I am able to get the output from the link : http://www.cafewebmaster.com/online_tools/utf_decode
Please try to use the link and you will understand my concern.
It would be highly appreciated if anyone can give some hint, tutorial or point me in the right direction.
Regards
To turn an NSString object into a UTF8 C-string, use UTF8String
char *utf8string = [#"A string with ümläuts" UTF8String];
To turn a UTF8 C-string into an NSString object, use stringWithUTF8String: or initWithUTF8String:
NSString *string = [NSString stringWithUTF8String:utf8string];
Note that NSString objects are implemented as UTF-16, so you can't really have a "UTF-8 NSString" (and the encoding should be treated as an implementation detail, anyway).
Instead of
char *utf8string = [#"A string with ümläuts" UTF8String];
This should be
const char *utf8string = [#"A string with ümläuts" UTF8String];
Otherwise you have an incompatible type issue.