PDA

View Full Version : Fixed Length String??




Fritzables
Mar 28, 2012, 01:22 AM
I am having trouble working out how to create a fixed length string.

I have a need to create a NSString that has a maximum number of characters of 12.

If I assign a sting that has 6 characters then the balance of the would need to be 6 blank spaces for example.

Is this at all possible or will it always be trimmed.


Pete



jared_kipe
Mar 28, 2012, 01:53 AM
Perhaps thing somewhat backwards. What if you started with a 12 space NSString and then replaced a portion of it with your possibly less than 12 space string.

Fritzables
Mar 28, 2012, 02:00 AM
G'Day Jared,

I will give it a try and see what comes out the other end.

Fritzables
Mar 28, 2012, 06:32 AM
Well.... I managed to find something that works for me. I used a variation of:

NSString *mycall = [NSString stringWithFormat:@"%@",[stnCallSign stringValue]];
mycall = [mycall stringByPaddingToLength:12 withString:@" " startingAtIndex:0];


Pete

jared_kipe
Mar 28, 2012, 09:51 AM
I guess thats one way to do it. I was thinking something like this. The NSString class never ceases to amaze!


NSString *blanks = @" ";
NSString *insert = @"test";
NSString *out = [blanks stringByReplacingCharactersInRange: NSMakeRange( 0, [insert length]) withString: insert];


With appropriate boundary conditions to ensure that insert wasn't longer than blanks, etc...


NSString *mycall = [NSString stringWithFormat:@"%@",[stnCallSign stringValue]];

Why would you go through this trouble instead of just..

NSString *mycall = [stnCallSign retain];

or just..

NSString *mycall = [stnCallSign stringByPaddingToLength:12 withString:@" " startingAtIndex:0];

chown33
Mar 28, 2012, 10:20 AM
Why would you go through this trouble instead of just..

NSString *mycall = [stnCallSign retain];

or just..

NSString *mycall = [stnCallSign stringByPaddingToLength:12 withString:@" " startingAtIndex:0];

stnCallSign probably isn't an NSString. Note the use of stringValue in the OP code. This is not an NSString method AFAIK.

Also, retain is unbalanced, so that's a leak.

You probably meant something more like this:

NSString *mycall = [stnCallSign stringValue];

or this:

NSString *mycall = [[stnCallSign stringValue] stringByPaddingToLength:12 withString:@" " startingAtIndex:0];


But I do agree with you that the senseless use of stringWithFormat: should be burned with fire. Because the short analysis of the problem is: "It's already an NSString".

jared_kipe
Mar 28, 2012, 11:08 AM
stnCallSign probably isn't an NSString. Note the use of stringValue in the OP code. This is not an NSString method AFAIK.

Also, retain is unbalanced, so that's a leak.

You probably meant something more like this:

NSString *mycall = [stnCallSign stringValue];

or this:

NSString *mycall = [[stnCallSign stringValue] stringByPaddingToLength:12 withString:@" " startingAtIndex:0];


But I do agree with you that the senseless use of stringWithFormat: should be burned with fire. Because the short analysis of the problem is: "It's already an NSString".

Yeah, obviously I meant the interim -(NSString *)stringValue;
And I certainly didn't mean to unbalance a retain, I just meant for him to think about ownership, like -retain or -copy instead of +stringWithFormat:;