Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: want backslash in NSString



On Sunday, December 30, 2001, at 08:02 PM, Christopher Anand wrote:

How do I get a backslash in an NSString?

I have tried the following:
...
string = [NSString stringWithCString: "\\include" length:8];
NSLog(string);

This is a right way to create an NSString with the value
\include
. If you're just coding an NSString constant, of course, you'd just say @"\\include".

Take notice that the first argument to NSLog is an NSString containing printf-style format. Using the format parameter to output the contents of NSString variables _almost_ works, just as
printf(stringvar);
_almost_ works with char * variables (it misbehaves if stringvar points to a string containing a %). So you'd prefer
NSLog(@"%@", string);
or
NSLog(@"%s", [string cString]);
. And instead of stringWithCString:length:, you'd prefer the NSString constant @"\\include".

2001-12-30 20:41:38.145 CircleView[403] \\\\include

Ouch. Looks like a bug in NSLog to me (NSLog(@"%@", @"\\include") does the same), though older heads can probably adduce a Very Good Reason for this behavior.
printf([@"\\include" cString]);
prints
\include
as expected.

-- F


References: 
 >want backslash in NSString (From: Christopher Anand <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.