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
38
votes
8 answers

NSString is empty

How do you test if an NSString is empty? or all whitespace or nil? with a single method call?
Yazzmi
  • 1,541
  • 5
  • 18
  • 33
38
votes
5 answers

NSString to NSDate

I got a string that contains the current date by using this : NSString *date = [[NSDate date] description]; At a different point I want to retrieve the date from this string and I used the following code: [NSDateFormatter…
Minar
  • 483
  • 1
  • 6
  • 13
37
votes
6 answers

Find all locations of substring in NSString (not just first)

There is a substring that occurs in a string several times. I use rangeOfString, but it seems that it can only find the first location. How can I find all the locations of the substring? NSString *subString1 = @""; NSString *subString2 =…
forest
  • 411
  • 1
  • 4
  • 6
37
votes
2 answers

How to search a subString from a NSString with case-insensitive

I knew a instance method of NSString. - (NSRange)rangeOfString:(NSString *)aString My source string looks like this: "abcdEFGhi" When I use [srcStr.rangeOfString:@"efg"];, there is no substring was found. How can I solve this problem?
Daemon
  • 439
  • 1
  • 4
  • 8
37
votes
2 answers

Replace occurrences of NSString - iPhone

I have a long NSString in which I m trying to replace special characters. Part of my string looks like this: "veau (c\u00f4telette)","veau (filet)","agneau (gigot)","agneau (c\u00f4telette)","b**\u0153**uf (hach\u00e9)","porc (hach\u00e9)" I would…
Nate
  • 7,606
  • 23
  • 72
  • 124
36
votes
1 answer

How to convert "SEL" and "id" to NSString?

id parent; SEL selector; // lot's of code... if ([parent respondsToSelector:selector]) { } else { // This doesn't work: NSString *errorMessage = [NSString stringWithFormat:@"%@ in class %@ doesn't exist!", selector, parent]; } How do I…
Manni
  • 11,108
  • 15
  • 49
  • 67
36
votes
3 answers

converting data back into a string

So I have this: NSData *charlieSendData = [[charlieImputText stringValue] dataUsingEncoding:NSUTF8StringEncoding]; I know how to convert NSStrings to data but how I convert data back to an NSString? Elijah
objectiveccoder001
  • 2,981
  • 10
  • 48
  • 72
36
votes
2 answers

Using NSPredicate to determine if a string equals another string

I have an NSArray of CalEvents returned with the [CalCalendarStore eventPredicateWithStartDate] method. From the events returned, I am trying to keep only those in which the title of the event == @"on call" (case-insensitive). I am able to keep in…
Garry Pettet
  • 8,096
  • 22
  • 65
  • 103
36
votes
4 answers

How do I use the NSString draw functionality to create a UIImage from text

I would like to draw the content of a NSString variable in a UIImage, but I have absolutely no idea how to do this. I need to write a method that would receive a NSString as parameter and return a UIImage with the text drawn into it.
Jab
  • 26,853
  • 21
  • 75
  • 114
35
votes
6 answers

How to insert a character into a NSString

How do I insert a space to a NSString. I need to add a space at index 5 into: NString * dir = @"abcdefghijklmno"; To get this result: abcde fghijklmno with: NSLOG (@"%@", dir);
JohnPortella
  • 1,791
  • 5
  • 21
  • 30
35
votes
3 answers

How to capture last 4 characters from NSString

I am accepting an NSString of random size from a UITextField and passing it over to a method that I am creating that will capture only the last 4 characters entered in the string. I have looked through NSString Class Reference library and the only…
C.Johns
  • 10,185
  • 20
  • 102
  • 156
35
votes
5 answers

NSString is integer?

How to check if the content of a NSString is an integer value? Is there any readily available way? There got to be some better way then doing something like this: - (BOOL)isInteger:(NSString *)toCheck { if([toCheck intValue] != 0) { return…
Carlos Barbosa
  • 3,083
  • 5
  • 30
  • 30
35
votes
1 answer

Converting an NSString* to char?

How can I convert a NSString* into a char? EDIT: Thanks to Vladimir for getting me there! NSString *myString = @"HelloWorld"; const char *stringAsChar = [myString cStringUsingEncoding:[NSString defaultCStringEncoding]]; Hope this helps someone in…
ingh.am
  • 25,981
  • 43
  • 130
  • 177
35
votes
9 answers

How to capitalize the first word of the sentence in Objective-C?

I've already found how to capitalize all words of the sentence, but not the first word only. NSString *txt =@"hi my friends!" [txt capitalizedString]; I don't want to change to lower case and capitalize the first char. I'd like to capitalize the…
wal
  • 2,044
  • 3
  • 18
  • 22
34
votes
6 answers

Find one string in another with case insensitive in Objective-C

My question is similar to How do I check if a string contains another string in Objective-C? How can I check if a string (NSString) contains another smaller string but with ignoring case? NSString *string = @"hello bla bla"; I was hoping for…
Jim
  • 8,874
  • 16
  • 68
  • 125