Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

netytan

macrumors 6502
Original poster
May 23, 2004
254
0
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Define a recursive function?
Code:
-(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

macrumors 6502
Original poster
May 23, 2004
254
0
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
netytan said:
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

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
netytan said:
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:

Code:
//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
 

netytan

macrumors 6502
Original poster
May 23, 2004
254
0
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

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
netytan said:
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:

Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.