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
44
votes
2 answers

Break up long formatted NSString over multiple lines

Given the following line of Objective-C code: [NSString stringWithFormat:@"\n Elapsed Time \n Battery Level: \n Torque: \n Energy Used \n Energy Regenerated:\n Cadence: \n Battery Temp: \n Motor Temp: \n Incline: \n Speed MPH: \n Speed KPH:\n…
Sabobin
  • 4,256
  • 4
  • 27
  • 33
44
votes
8 answers

Cocoa - Trim all leading whitespace from NSString

(have searched, but not been able to find a simple solution to this one either here, or in Cocoa docs) Q. How can I trim all leading whitespace only from an NSString? (i.e. leaving any other whitespace intact.) Unfortunately, for my purposes,…
SirRatty
  • 2,338
  • 5
  • 25
  • 37
44
votes
5 answers

Returning an NSString from an NSError

I am using the NSURLRequest class in my iPhone app, and the method that calls it returns an NSString which is great for when the connection goes through ok, but the issue is I need to convert the NSError into an NSString so I can either return it…
tarnfeld
  • 25,992
  • 41
  • 111
  • 146
44
votes
7 answers

Is it possible to include a quotation mark as part of an nsstring?

I have a label that displays inches. I would like to display the number with the inch symbol (") or quotation mark. Can I do this with an nsstring? Thanks!
Jonah
  • 4,810
  • 14
  • 63
  • 76
43
votes
6 answers

Converting between NSData and base64 strings

What is the easiest and fastest code to do a conversion between NSData and a base64 string? I've read a bunch of solutions at SO and mostly they involve in adding another class etc. I found a great solution here but it's too complex.
aherlambang
  • 14,290
  • 50
  • 150
  • 253
43
votes
3 answers

NSData to display as a string

I am building an iPhone app and stuck with the following: unsigned char hashedChars[32]; CC_SHA256([inputString UTF8String], [inputString lengthOfBytesUsingEncoding:NSASCIIStringEncoding], hashedChars); NSData *hashedData = [NSData…
topace
  • 1,791
  • 3
  • 17
  • 23
43
votes
8 answers

How to make an NSString path (file name) safe

I'm using very tricky fighting methods :) to make a string like Fi?le*/ Name safe for using as a file name like File_Name. I'm sure there is a cocoa way to convert it. And I'm sure the best place to ask is here :) Thank you!
cocoafan
  • 4,884
  • 4
  • 37
  • 45
42
votes
5 answers

How to find beginswith word in a string using NSPredicate?

I am searching for a solution on how to format NSPredicate to search correct word in a string of text. Currently I am using this code: NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(content CONTAINS[c] %@)",…
azia
  • 505
  • 1
  • 5
  • 11
41
votes
2 answers

Search through NSString using Regular Expression

How might I go about searching/enumerating through an NSString using a regular expression? A regular expression such as: /(NS|UI)+(\w+)/g.
Joshua
  • 15,200
  • 21
  • 100
  • 172
40
votes
7 answers

Objective-C: Find numbers in string

I have a string that contains words as well as a number. How can I extract that number from the string? NSString *str = @"This is my string. #1234"; I would like to be able to strip out 1234 as an int. The string will have different numbers and…
Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412
40
votes
2 answers

How to use drawInRect:withAttributes: instead of drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment: in iOS 7

This method is deprecated in iOS 7.0: drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment: Now use drawInRect:withAttributes: instead. I can't find the attributeName of fontSize and baselineAdjustment. Edit Thanks @Puneet…
Vincent Sit
  • 2,214
  • 1
  • 24
  • 27
39
votes
10 answers

Number of Occurrences of a Character in NSString

I have an NSString or NSMutableString and would like to get the number of occurrences of a particular character. I need to do this for quite a few characters -- uppercase English characters in this case -- so it would be nice for it to be quick.
Elliot
  • 6,086
  • 11
  • 45
  • 57
39
votes
4 answers

Simple string concatenation in Objective C

I have an NSString named 'you' with value "This is a you string!". I want to concat "123" in 'you', how can I do it? I am using this code and it is giving an error. you=[you stringByAppendingString:@"123"];
Umair Khan Jadoon
  • 2,874
  • 11
  • 42
  • 63
39
votes
4 answers

Convert NSMutableAttributedString to NSString

Can we not convert NSMutableAttributedString to NSString? I have two NSMutableAttributedStrings and I am appending the 2nd string onto 1st as below: [string1 appendAttributedString:string2]; Since I have to display string1 on a label I…
tech_human
  • 6,592
  • 16
  • 65
  • 107
38
votes
2 answers

NSString stringWithFormat adding a percent

How can I add a percent to my stringWithFormat function? For example, I'd like to have the following: float someFloat = 40.233f; NSString *str = [NSString stringWithFormat:@"%.02f%",someFloat]; This should cause the string to be: 40.23% But it is…
CodeGuy
  • 28,427
  • 76
  • 200
  • 317