jac300
jac300

Reputation: 5222

Trying to Format a String that is Not a String

I have an application in Xcode 4.5. My application allows the user to make a post on their Facebook wall. Each post returns a unique post identification number that is of variable type 'id'. For the purposes of my app, I need to capture this post identification number, and reformat it. The post id has the general format: "12345_678910" (the quotations are part of the format). What I need to do is create a new string that includes only part of the post id; specifically (from this example) just the 678910 - in other words, everything after the under bar and before the last quotation.

Here is the strange part: when I create a fake post id in the form of an NSString with the same type of format as the actual post identifications, I can format that fake post id to get exactly what I want. But when I run the same code on the 'id' type variable that is the actual post id, I get different results! See example below:

//result is the post id of type 'id' (the console displays it as "12345_67890")

NSString *resultString = [NSString stringWithFormat:@"%@",result];
         NSArray *resultComponents = [resultString componentsSeparatedByString:@"_"];
         NSString *remainingString = [resultComponents lastObject]; //this works
         NSString *finalString = [remainingString substringToIndex:[remainingString  
            length]-1]; //this doesn't work
         [DataController dc].postIDData = finalString;
         NSLog(@"%@",[DataController dc].postIDData); //the console reads 67890"
        }

So the above code yields the part of the number after the under bar (which I want) but still includes the quote! If I use this same code on a fake result string of type NSString, then I do get just 67890. What is also strange is that when I do an NSLog to count the number of characters in the "resultString" [resultString length], it gives me a larger value than the numbers of characters I physically count. I don't really understand what is going on here nor do I have any idea how to fix it. Any help is appreciated.

Upvotes: 0

Views: 55

Answers (1)

Carl Veazey
Carl Veazey

Reputation: 18363

After splitting on the underscore, try

finalString = [remainingString stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];

I'd suspect there is trailing white space in the response.

Upvotes: 1

Related Questions