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
74
votes
4 answers

How to get a single NSString character from an NSString

I want to get a character from somewhere inside an NSString. I want the result to be an NSString. This is the code I use to get a single character at index it: [[s substringToIndex:i] substringToIndex:1] Is there a better way to do it?
node ninja
  • 31,796
  • 59
  • 166
  • 254
74
votes
9 answers

Strip Non-Alphanumeric Characters from an NSString

I'm looking for a quick and easy way to strip non-alphanumeric characters from an NSString. Probably something using an NSCharacterSet, but I'm tired and nothing seems to return a string containing only the alphanumeric characters in a string.
Jeff Kelley
  • 19,021
  • 6
  • 70
  • 80
72
votes
9 answers

How to add commas to number every 3 digits in Objective C?

If I have a number int aNum = 2000000 how do I format this so that I can display it as the NSString 2,000,000?
RexOnRoids
  • 14,002
  • 33
  • 96
  • 136
72
votes
6 answers

Shortcut to generate an NSRange for entire length of NSString?

Is there a short way to say "entire string" rather than typing out: NSMakeRange(0, myString.length)] It seems silly that the longest part of this kind of code is the least important (because I usually want to search/replace within entire…
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
71
votes
6 answers

UILabel - string as text and links

I have a UILabel whose text I am getting from a server. Some of the text is to be identified as links, and on touching those links some action should be performed. e.g. NSString *str = @"My phone number is 645-345-2345 and my address is xyz"; This…
Nitish
  • 13,845
  • 28
  • 135
  • 263
71
votes
11 answers

Read a text file line by line in Swift?

I just started learning Swift. I have got my code to read from the text file, and the App displays the content of the entire text file. How can I display line by line and call upon that line multiple times? TextFile.txt contains the following: 1.…
ScarletEnvy
  • 871
  • 1
  • 7
  • 14
70
votes
2 answers

how to check if NSString = a specific string value?

Hi I am woundering if you can check to see if a NSString equals a specific value say for instance a name of a person? I am thinking along the lines of if (mystring == @"Johns"){ //do some stuff in here }
C.Johns
  • 10,185
  • 20
  • 102
  • 156
70
votes
2 answers

Objective C: convert a NSMutableString in NSString

I have an NSMutableString, how can I convert it to an NSString?
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
70
votes
6 answers

NSString containsString crashes

I'm trying to filter an array according to one of it's string fields. Both nameLower and filterLower has NSString value inside, and yet i keep getting: __NSCFString containsString:]: unrecognized selector sent to instance 0x7f876b79e160 -(void)…
Asaf Nevo
  • 11,338
  • 23
  • 79
  • 154
70
votes
4 answers

How to remove first 3 characters from NSString?

I have a string like this "A. rahul VyAs" and i want to remove "A. " and the space after the "A." so that new string would be "rahul VyAs" How do i achieve this?
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
69
votes
6 answers

Converting a string to an NSDate

How is it possible to convert a string to an NSDate on iOS?
hugo411
  • 320
  • 1
  • 12
  • 29
68
votes
12 answers

Adding a line break to a UITextView

I have a UITextView that takes an NSString with formatting stringWithUTF8String. It is getting its values from a database and I want the text in the database to be rendered with breaks within the text. I tried using \n to do this but it gets…
Mark Cicero
  • 737
  • 1
  • 6
  • 5
67
votes
2 answers

How to use printf with NSString

I need to use something like NSLog but without the timestamp and newline character, so I'm using printf. How can I use this with NSString?
node ninja
  • 31,796
  • 59
  • 166
  • 254
66
votes
16 answers

enum Values to NSString (iOS)

I have an enum holding several values: enum {value1, value2, value3} myValue; In a certain point in my app, I wish to check which value of the enum is now active. I'm using NSLog but I'm not clear on how to display the current value of the enum…
Ohad Regev
  • 5,641
  • 12
  • 60
  • 84
62
votes
3 answers

How to check the last char of an NSString

I want to ask a question about the NSString * in objective C. Can I check the last char of a NSString * object? Example: NSString* data = @"abcde,"; if(data is end with ',') // I don't know this part // do sth
Questions
  • 20,055
  • 29
  • 72
  • 101