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
141
votes
18 answers

Objective-C: Reading a file line by line

What is the appropriate way of dealing with large text files in Objective-C? Let's say I need to read each line separately and want to treat each line as an NSString. What is the most efficient way of doing this? One solution is using the NSString…
Jonathan
137
votes
7 answers

How to convert NSNumber to NSString

So I have an NSArray "myArray" with NSNumbers and NSStrings. I need them in another UIView so i go like this: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *details =…
dav3
  • 1,447
  • 2
  • 10
  • 9
136
votes
11 answers

Remove all whitespaces from NSString

I've been trying to get rid of the white spaces in an NSString, but none of the methods I've tried worked. I have "this is a test" and I want to get "thisisatest". I've used whitespaceCharacterSet, which is supposed to eliminate the white…
marsalal1014
  • 1,897
  • 3
  • 17
  • 24
127
votes
5 answers

AES Encryption for an NSString on the iPhone

Can anybody point me in the right direction to be able to encrypt a string, returning another string with the encrypted data? (I've been trying with AES256 encryption.) I want to write a method which takes two NSString instances, one being the…
Boz
  • 1,646
  • 5
  • 15
  • 15
125
votes
7 answers

Detecting if an NSString contains...?

How can I detect if a string contains a certain word? For example, I have a string below which reads: @"Here is my string." I'd like to know if I can detect a word in the string, such as "is" for example.
Alex
  • 1,251
  • 2
  • 8
  • 3
124
votes
9 answers

Objective-C formatting string for boolean?

What formatter is used for boolean values? EDIT: Example: NSLog(@" ??", BOOL_VAL);, what is ?? ?
Moshe
  • 57,511
  • 78
  • 272
  • 425
122
votes
13 answers

Collapse sequences of white space into a single character and trim string

Consider the following example: " Hello this is a long string! " I want to convert that to: "Hello this is a long string!"
Paul
114
votes
2 answers

Get parts of a NSURL in objective-c

I have an NSString with the value of http://digg.com/news/business/24hr How can I get everything before the 3rd level? http://digg.com/news/
Melina
  • 1,463
  • 4
  • 13
  • 13
111
votes
6 answers

How to convert std::string to NSString?

I am trying to convert a standard std::string into an NSString but I'm not having much luck. I can convert successfully from an NSString to a std::string with the following code NSString *realm = @"Hollywood"; std::string REALM = [realm…
Anthony McCormick
  • 2,724
  • 3
  • 25
  • 27
108
votes
22 answers

Remove HTML Tags from an NSString on the iPhone

There are a couple of different ways to remove HTML tags from an NSString in Cocoa. One way is to render the string into an NSAttributedString and then grab the rendered text. Another way is to use NSXMLDocument's -objectByApplyingXSLTString method…
lfalin
  • 4,219
  • 5
  • 31
  • 57
106
votes
3 answers

String comparison in Objective-C

I've currently got a webserver set up which I communicate over SOAP with my iPhone app. I am returning a NSString containing a GUID and when I attempt to compare this with another NSString I get some strange results. Why would this not fire? Surely…
ingh.am
  • 25,981
  • 43
  • 130
  • 177
101
votes
10 answers

NSString with \n or line break

Does anyone know how to use line breaks in NSString? I need to do something like this - [NSString stringWithFormat:@"%@,\n%@", mystring1,mystring2];
Dave
  • 4,038
  • 9
  • 45
  • 57
98
votes
5 answers

NSString: isEqual vs. isEqualToString

What is the difference between isEqual: and isEqualToString:? Why are classes adding isEqualTo* methods (isEqualToArray for NSArray, isEqualToData for NSData, ...) instead of just overriding isEqual: ?
Jaka Jančar
  • 11,386
  • 9
  • 53
  • 74
97
votes
6 answers

Remove characters from NSString?

NSString *myString = @"A B C D E F G"; I want to remove the spaces, so the new string would be "ABCDEFG".
Raju
  • 3,459
  • 12
  • 37
  • 30
96
votes
1 answer

NSString to CFStringRef and CFStringRef to NSString in ARC?

I am trying to understand the correct way of getting an NSString from a CFStringRef in ARC? Same for going the opposite direction, CFStringRef to NSString in ARC? What is the correct way to do this without creating memory leaks?
zumzum
  • 17,984
  • 26
  • 111
  • 172