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
95
votes
19 answers

Finding out whether a string is numeric or not

How can we check if a string is made up of numbers only. I am taking out a substring from a string and want to check if it is a numeric substring or not. NSString *newString = [myString substringWithRange:NSMakeRange(2,3)];
Abhinav
  • 37,684
  • 43
  • 191
  • 309
94
votes
8 answers

URLWithString: returns nil

it may be very easy, but I don't seems to find out why is URLWithString: returning nil here. //localisationName is a arbitrary string here NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];…
gcamp
  • 14,622
  • 4
  • 54
  • 85
93
votes
3 answers

Make a two-digit string from a single-digit integer

How can I have a two-digit integer in a a string, even if the integer is less than 10? [NSString stringWithFormat:@"%d", 1] //should be @"01"
aneuryzm
  • 63,052
  • 100
  • 273
  • 488
88
votes
3 answers

String search in string array in objective c

I want to search a specific string in the array of strings in objective c. Can somebody help me in this regard?
IDev
  • 1,221
  • 1
  • 11
  • 25
86
votes
5 answers

Converting NSString to NSDictionary / JSON

I have the following data saved as an NSString : { Key = ID; Value = { Content = 268; Type = Text; }; }, { Key = ContractTemplateId; Value = { Content = 65; Type = Text; …
GuybrushThreepwood
  • 5,598
  • 9
  • 55
  • 113
84
votes
7 answers

Understanding NSString comparison

Both the following comparisons evaluate to true: 1) @"foo" == @"foo"; 2) NSString *myString1 = @"foo"; NSString *myString2 = @"foo"; myString1 == myString2; However, there are definitely times where two NSStrings cannot be compared using the…
Yarin
  • 173,523
  • 149
  • 402
  • 512
84
votes
7 answers

How to convert from int to string in objective c: example code

I am trying to convert from an int to a string but I am having trouble. I followed the execution through the debugger and the string 'myT' gets the value of 'sum' but the 'if' statement does not work correctly if the 'sum' is 10,11,12. Should I…
john
  • 1,189
  • 2
  • 15
  • 20
83
votes
3 answers

Check if NSString instance is contained in an NSArray

I have an array with a bunch of strings and I want to check if a certain string is contained in the array. If I use the containsObject: message on the array, I'm getting correct results. Do all NSString objects with the same string point to the same…
the Reverend
  • 12,305
  • 10
  • 66
  • 121
83
votes
6 answers

sizeWithFont method is deprecated. boundingRectWithSize returns an unexpected value

In iOS7, sizeWithFont is deprecated, so I am using boundingRectWithSize(which returns a CGRect value). My code: UIFont *fontText = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16]; // you can use your font. CGSize…
Nirav Jain
  • 5,088
  • 5
  • 40
  • 61
82
votes
22 answers

Check that a input to UITextField is numeric only

How do I validate the string input to a UITextField? I want to check that the string is numeric, including decimal points.
g.revolution
  • 11,962
  • 23
  • 81
  • 107
81
votes
5 answers

How to get substring of NSString?

If I want to get a value from the NSString @"value:hello World:value", what should I use? The return value I want is @"hello World".
Csabi
  • 3,097
  • 17
  • 59
  • 107
79
votes
2 answers

Split NSString multiple times on the same separator

I am currently receiving a string like this: @"Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54" And I am splitting it like this: testArray = [[NSArray alloc] init]; NSString *testString = [[NSString alloc]…
Sam Parrish
  • 1,377
  • 1
  • 11
  • 17
79
votes
11 answers

Any way to bold part of a NSString?

Is there any way to bold only part of a string? For example: Approximate Distance: 120m away Thanks!
Zhen
  • 12,361
  • 38
  • 122
  • 199
79
votes
7 answers

Convert NSString to NSInteger?

I want to convert string data to NSInteger.
The deals dealer
  • 1,006
  • 2
  • 8
  • 16
75
votes
6 answers

Remove last character of NSString

I've got some trouble 'ere trying to remove the last character of an NSString. I'm kinda newbie in Objective-C and I have no idea how to make this work. Could you guys light me up?
markus
  • 1,011
  • 2
  • 10
  • 17