Questions tagged [nsstring]

NSString is the plain-text character-string class in Cocoa and Cocoa Touch. See also NSMutableString, NSData and NSMutableData (for objects that contain bytes rather than human-language characters), and NSAttributedString and NSMutableAttributedString (for rich-text strings).

NSString is the plain-text character-string class in Cocoa () and Cocoa Touch ().

An NSString is immutable, unless it's an NSMutableString (). NSMutableString is a subclass of NSString.

You will never work with a direct instance of NSString or NSMutableString. Both classes, like most of the other property-list classes, are members of a class cluster.

NSStrings contain human-language characters; you should not attempt to use an NSString to store bytes, including pixel data and XML or HTML markup. (“String” in Cocoa strictly refers to human text; it does not call a byte array a “string” like Python 2 and some other environments do.) To make or receive an object bytes, you should use NSData or NSMutableData (note that, for example, this is what NSXMLParser, WebFrame, and UIWebView take for the XML/HTML source of the document, and what NSURLConnection gives you as you download a resource).

An NSString is always plain text. For rich text, use NSAttributedString or its mutable subclass. Both have always been available on Mac OS X, and have been available on iOS since 3.2.

NSString is toll-free bridged with CFString, which means you can pass an NSString to CF functions expecting a CFString, and receive a CFString and treat it as an NSString. The same goes for the mutable subclass.

Docs:

7124 questions
244
votes
15 answers

How to create a String with format?

I need to create a String with format which can convert Int, Int64, Double, etc types into String. Using Objective-C, I can do it by: NSString *str = [NSString stringWithFormat:@"%d , %f, %ld, %@", INT_VALUE, FLOAT_VALUE, DOUBLE_VALUE,…
Apurv
  • 17,116
  • 8
  • 51
  • 67
233
votes
17 answers

Converting NSString to NSDate (and back again)

How would I convert an NSString like "01/02/10" (meaning 1st February 2010) into an NSDate? And how could I turn the NSDate back into a string?
cannyboy
  • 24,180
  • 40
  • 146
  • 252
215
votes
10 answers

How to check if NSString begins with a certain character

How do you check if an NSString begins with a certain character (the character *). The * is an indicator for the type of the cell, so I need the contents of this NSString without the *, but need to know if the * exists.
Xetius
  • 44,755
  • 24
  • 88
  • 123
201
votes
24 answers

How do I URL encode a string

I have a URL string (NSString) with spaces and & characters. How do I url encode the entire string (including the & ampersand character and spaces)?
xonegirlz
  • 8,889
  • 19
  • 69
  • 127
189
votes
9 answers

Convert NSArray to NSString in Objective-C

I am wondering how to convert an NSArray [@"Apple", @"Pear ", 323, @"Orange"] to a string in Objective-C.
alexyorke
  • 4,224
  • 3
  • 34
  • 55
187
votes
31 answers

How can I convert my device token (NSData) into an NSString?

I am implementing push notifications. I'd like to save my APNS Token as a String. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken { NSString *tokenString = [NSString…
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
181
votes
9 answers

Creating NSData from NSString in Swift

I'm trying to ultimately have an NSMutableURLRequest with a valid HTTPBody, but I can't seem to get my string data (coming from a UITextField) into a usable NSData object. I've seen this method for going the other way: NSString(data data: NSData!,…
Jackson Egan
  • 2,715
  • 4
  • 18
  • 26
160
votes
5 answers

How to see if an NSString starts with a certain other string?

I am trying to check to see if a string that I am going to use as URL starts with http. The way I am trying to check right now doesn't seem to be working. Here is my code: NSMutableString *temp = [[NSMutableString alloc]…
Rob
  • 2,319
  • 4
  • 20
  • 18
158
votes
5 answers

How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)

In my iOS 5 app, I have an NSString that contains a JSON string. I would like to deserialize that JSON string representation into a native NSDictionary object. "{\"password\" : \"1234\", \"user\" : \"andreas\"}" I tried the following…
Andreas
  • 397
  • 4
  • 18
  • 37
158
votes
22 answers

Remove all but numbers from NSString

I have an NSString (phone number) with some parenthesis and hyphens as some phone numbers are formatted. How would I remove all characters except numbers from the string?
Ben Harris
  • 5,664
  • 3
  • 30
  • 27
150
votes
9 answers

How do I convert NSInteger to NSString datatype?

How does one convert NSInteger to the NSString datatype? I tried the following, where month is an NSInteger: NSString *inStr = [NSString stringWithFormat:@"%d", [month intValue]];
user141302
146
votes
8 answers

Replacement for deprecated -sizeWithFont:constrainedToSize:lineBreakMode: in iOS 7?

In iOS 7, the method: - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode and the method: - (CGSize)sizeWithFont:(UIFont *)font are deprecated. How can I replace CGSize…
user_Dennis_Mostajo
  • 2,279
  • 4
  • 28
  • 38
142
votes
7 answers

Split an NSString to access one particular piece

I have a string like this: @"10/04/2011" and I want to save only the "10" in another string. How can I do that?
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
142
votes
3 answers

Capitalize or change case of an NSString in Objective-C

I was wondering how to capitalize a string found in an object in an NSMutableArray. An NSArray contains the string 'April' at index 2. I want this to be changed to 'APRIL'. Is there something simple like this? viewNoteDateMonth.text = [[displayDate…
n.evermind
  • 11,944
  • 19
  • 78
  • 122
141
votes
13 answers

Objective-C and Swift URL encoding

I have a NSString like this: http://www. but I want to transform it to: http%3A%2F%2Fwww. How can I do this?
Usi Usi
  • 2,967
  • 5
  • 38
  • 69