Rob
Rob

Reputation: 16002

NSString length not correct when checked?

I have a problem with this function :

- (bool) test : (NSString *) chaine
{
    NSLog(@"%i",[chaine length]);
    if([chaine length] == 19) NSLog(@"Test");
}

I correctly have 19 in my log, but not "Test". So do you know what's wrong ?

Thanks a lot

Upvotes: 1

Views: 339

Answers (3)

Meet
Meet

Reputation: 4934

I tried this

- (void)testFunction{
NSString * aStrFunc= @"StackOverflow";

NSLog(@"%d",[aStrFunc length]);

NSLog(@"%@",[aStrFunc length]==13?@"test right":@"test not right");

}

Upvotes: 3

AleyRobotics
AleyRobotics

Reputation: 970

try this

NSData* data=[chaine dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"@s",[data bytes]);//watch the string's bytes possibly you have some unprintable symbols here
if ([data length] == 19) {
NSLog(@"Test");
}

Upvotes: -2

Pavel Petrovich
Pavel Petrovich

Reputation: 764

Try to use this code:

- (bool) test : (NSString *) chaine
{
    //CGFloat len = (float)[chaine length];
    NSUInteger len = [chaine length];
    NSLog(@"%i", len);
    //if (19.0f == len)
    if (19 == len)
    {
        NSLog(@"Test");
    }
}

Upvotes: 0

Related Questions