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

Replacing one character in a string in Objective-C

Hoping somebody can help me out - I would like to replace a certain character in a string and am wondering what is the best way to do this? I know the location of the character, so for example, if I want to change the 3rd character in a string from…
RanLearns
  • 4,086
  • 5
  • 44
  • 81
52
votes
3 answers

Get last 2 characters of a string?

Say I have a string: NSString *state = @"California, CA"; Can someone please tell me how to extract the last two characters from this string (@"CA" in this example).
dpigera
  • 3,339
  • 5
  • 39
  • 60
51
votes
10 answers

How to make first letter uppercase in a UILabel?

I'm developing an iPhone app. In a label, I want to show an user's first letter of the name uppercase. How do I do that?
HardCode
  • 2,025
  • 4
  • 33
  • 55
50
votes
7 answers

Finding a substring in a NSString object

I have an NSString object and I want to make a substring from it, by locating a word. For example, my string is: "The dog ate the cat", I want the program to locate the word "ate" and make a substring that will be "the cat". Can someone help me out…
Sagiftw
  • 1,658
  • 4
  • 21
  • 25
49
votes
10 answers

Check if an NSString is just made out of spaces

I want to check if a particular string is just made up of spaces. It could be any number of spaces, including zero. What is the best way to determine that?
Suchi
  • 9,989
  • 23
  • 68
  • 112
49
votes
4 answers

How to separate string by space using Objective-C?

Assume that I have a String like this: hello world this may have lots of sp:ace or little space I would like to seperate this String to this: @"hello", @"world", @"this", @"may", @"have", @"lots", @"of", @"sp:ace", @"or",…
user448236
  • 683
  • 2
  • 7
  • 13
47
votes
5 answers

Split up NSString using a comma

I have a JSON feed connected to my app. One of the items is lat & long separated by a comma. For example: "32.0235, 1.345". I'm trying to split this up into two separate values by splitting at the comma. Any advice? Thanks!!
Adam Storr
  • 1,438
  • 2
  • 21
  • 46
47
votes
4 answers

Is it possible to use SF Symbols outside of UIImage?

I'm confused on how to use SF Symbols in text in an iOS project. I have a UIButton that uses a symbol just fine using UIImage systemImageNamed:. I would like to display a message about that button with some text in another view. I thought I should…
Gene McCulley
  • 1,097
  • 1
  • 10
  • 15
47
votes
2 answers

align text using drawInRect:withAttributes:

In the iOS 5 version of my app I had: [self.text drawInRect: stringRect withFont: [UIFont fontWithName: @"Courier" size: kCellFontSize] lineBreakMode: NSLineBreakByTruncatingTail alignment:…
46
votes
8 answers

Replace only the first instance of a substring in an NSString

So if you have an NSString that goes: @"My blue car is bigger than my blue shoes or my blue bicycle"; I would like a method that replaces only the first instance of blue with green, to produce: @"My green car is bigger than my blue shoes or my blue…
Eric Brotto
  • 53,471
  • 32
  • 129
  • 174
46
votes
8 answers

NSString : easy way to remove UTF-8 accents from a string?

I want to change a sentence, for example: Être ou ne pas être. C'était là-bas. Would become: Etre ou ne pas etre. C'etait la-bas. Is there any easy way to do this with NSString? Or do I have to develop this on my own by checking each char?
Rob
  • 15,732
  • 22
  • 69
  • 107
45
votes
6 answers

Static NSString usage vs. inline NSString constants

In Objective-C, my understanding is that the directive @"foo" defines a constant NSString. If I use @"foo" in multiple places, the same immutable NSString object is referenced. Why do I see the following code snippet so often (for example in…
Trashpanda
  • 14,758
  • 3
  • 29
  • 18
45
votes
8 answers

Objective-C: Best way to extract substring from NSString?

I have a space-separated string like @"abc xyz http://www.example.com aaa bbb ccc". How can I extract the substring @"http://www.example.com" from it?
ThyPhuong
  • 483
  • 1
  • 4
  • 5
44
votes
6 answers

How do I remove leading & trailing whitespace of NSString inside an NSArray?

I have an NSArray declared as such: @property (nonatomic, strong) NSArray *arrayRefineSubjectCode; I have the array elements manually filled out as below: arrayRefineSubjectCode = [NSArray arrayWithObjects: @" …
shamsulfakhar
  • 563
  • 1
  • 4
  • 7
44
votes
4 answers

Converting HEX NSString To NSData

I'm trying to convert a Hex NSString to NSData (I'm using the below attached code). The following is the output: <00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000> which looks totally irrelevant to me. Any idea/ suggestions…
Pranav Jaiswal
  • 3,752
  • 3
  • 32
  • 50