PDA

View Full Version : Cocoa build strings!




netytan
Jan 17, 2005, 08:33 AM
Hey all,

Yet another question ;). Anyway, I'm looking for a way to append a variable number of '>' characters to a string. I know about the stringByAppendingString: method, but I'm not sure sure how to construct the @">>>>" strings to start with :rolleyes:.

I considered building the string with as a CString inside a loop but it just didn't seem to work for me. If someone could give me a small example I would much appreciate it!

Thanks :),

Mark.



robbieduncan
Jan 17, 2005, 08:50 AM
Define a recursive function?

-(NSString *) string:(NSString *) inString repeated:(int) times
{
if (times==1)
{
return inString;
}
else
{
return [inString stringByAppendingString:[self string:inString repeated:(times-1)]];
}
}


Note that I have not tested this as I am at work and don't have a Mac here :(

Also note that calling with times arguement as 0 or a negative number will cause bad things to happen at the moment!

netytan
Jan 17, 2005, 03:42 PM
Since i'm only going to be calling this once inside another action I'd prefer to stick with a loop, so I think I'll use the idea for append the char to a string :).

My inisial consern with this method of doing things was that that it might not be very efficent, would it be better to use a mutable string here?

Also, is there a way to count the number of times a character appears within an NSString/NSMutableString? Similar to count() in Python.

Thanks for the reply,

Mark.

robbieduncan
Jan 17, 2005, 03:53 PM
My inisial consern with this method of doing things was that that it might not be very efficent, would it be better to use a mutable string here?

Also, is there a way to count the number of times a character appears within an NSString/NSMutableString? Similar to count() in Python.


It'll probably be better using a NSMutableString as you won't need to create a new object every append. Reading the docs it does not look like there is any way to get the number of a certain character in a String. If you want there to be one add it with a category.

HexMonkey
Jan 17, 2005, 04:26 PM
My inisial consern with this method of doing things was that that it might not be very efficent, would it be better to use a mutable string here?

I tried it with both an NSString and an NSMutableString. If you are adding small numbers of ">"s, there'll be no noticeable difference in speed. But if you are potentially adding large numbers of them, a mutable string is much better. When adding 5000 ">"s, the mutable string method was almost 50 times as fast for me. Here's how I did it:

//Start with a non-mutable string "aString" and integer "numTimes"
NSMutableString* mutableString = [aString mutableCopy];
//You should release aString here if you are responsible for releasing it
int i;
for (i=0;i<numTimes;i++)
[mutableString appendString:@">"];
aString = mutableString;
//You should release aString at some point after here

robbieduncan
Jan 17, 2005, 04:31 PM
Good Work Dude!

Always worth testing these things out.

netytan
Jan 18, 2005, 01:25 PM
Very cool :D, and simpler than what I can up with. If noone minds I'll post my program after its done so people can give my any tips: what you would have done :).

Hex, how did you test the loops preformance? A util' I'm missing somewhere maybe :rolleyes:

Thanks again guys, very helpful!

Mark.

HexMonkey
Jan 18, 2005, 05:08 PM
Hex, how did you test the loops preformance? A util' I'm missing somewhere maybe :rolleyes:
There's several ways to do it, one of which is to just use NSLog before and after the code you're testing, and then calculate the difference between the times it prints. This is probably the easiest method, although the calculated time will always include some of the time it took to do the logging, so it's not very reliable.

A better way is to get the time before and after the code you're testing, and then log the difference. Since the time it takes to get the current time is minimal, this method is much more reliable. It's done as follows:

NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];

//Code you're timing goes here

NSTimeInterval endTime = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"Total time: %f seconds", endTime-startTime);

Either way, you should run the test several times and take the average to remove any spikes.