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
7,119
questions
1175
votes
30
answers
845k
views
Shortcuts in Objective-C to concatenate NSStrings
Are there any shortcuts to (stringByAppendingString:) string concatenation in Objective-C, or shortcuts for working with NSString in general?
For example, I'd like to make:
NSString *myString = @"...
1048
votes
14
answers
447k
views
Constants in Objective-C
I'm developing a Cocoa application, and I'm using constant NSStrings as ways to store key names for my preferences.
I understand this is a good idea because it allows easy changing of keys if ...
950
votes
14
answers
465k
views
How do I convert an NSString value to NSData?
How do I convert an NSString value to NSData?
690
votes
30
answers
297k
views
How do I test if a string is empty in Objective-C?
How do I test if an NSString is empty in Objective-C?
655
votes
27
answers
596k
views
How do I check if a string contains another string in Swift?
In Objective-C the code to check for a substring in an NSString is:
NSString *string = @"hello Swift";
NSRange textRange =[string rangeOfString:@"Swift"];
if(textRange.location != NSNotFound)
{
...
582
votes
18
answers
435k
views
How to convert an NSString into an NSNumber
How can I convert a NSString containing a number of any primitive data type (e.g. int, float, char, unsigned int, etc.)? The problem is, I don't know which number type the string will contain at ...
575
votes
7
answers
473k
views
Convert UTF-8 encoded NSData to NSString
I have UTF-8 encoded NSData from windows server and I want to convert it to NSString for iPhone. Since data contains characters (like a degree symbol) which have different values on both platforms, ...
469
votes
7
answers
137k
views
How to add percent sign to NSString
I want to have a percentage sign in my string after a digit. Something like this: 75%.
How can I have this done? I tried:
[NSString stringWithFormat:@"%d\%", someDigit];
But it didn't work for me.
372
votes
14
answers
180k
views
Trim spaces from end of a NSString
I need to remove spaces from the end of a string. How can I do that?
Example: if string is "Hello " it must become "Hello"
368
votes
3
answers
63k
views
"sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers" warning
I have Constants NSString, that I want to call like:
[newString isEqualToString:CONSTANT_STRING];
Any wrong code here?
I got this warning:
sending 'const NSString *' to parameter of type '...
348
votes
7
answers
232k
views
Convert an NSURL to an NSString
I have an app where the user can choose an image either from the built-in app images or from the iphone photo library. I use an object Occasion that has an NSString property to save the imagePath.
...
295
votes
16
answers
144k
views
NSRange to Range<String.Index>
How can I convert NSRange to Range<String.Index> in Swift?
I want to use the following UITextFieldDelegate method:
func textField(textField: UITextField!,
...
287
votes
6
answers
271k
views
String replacement in Objective-C
How to replace a character is a string in Objective-C?
266
votes
19
answers
329k
views
Convert NSDate to NSString
How do I convert, NSDate to NSString so that only the year in @"yyyy" format is output to the string?
253
votes
12
answers
117k
views
Case insensitive comparison NSString
Can anyone point me to any resources about case insensitive comparison in Objective C? It doesn't seem to have an equivalent method to str1.equalsIgnoreCase(str2)
246
votes
15
answers
344k
views
How to create a String with format?
I need to create a String with format which can convert Int, Int64, Double, etc types into String. Using Objective-C, I can do it by:
NSString *str = [NSString stringWithFormat:@"%d , %f, %ld, %@&...
233
votes
17
answers
168k
views
Converting NSString to NSDate (and back again)
How would I convert an NSString like "01/02/10" (meaning 1st February 2010) into an NSDate? And how could I turn the NSDate back into a string?
215
votes
10
answers
116k
views
How to check if NSString begins with a certain character
How do you check if an NSString begins with a certain character (the character *).
The * is an indicator for the type of the cell, so I need the contents of this NSString without the *, but need to ...
201
votes
24
answers
225k
views
How do I URL encode a string
I have a URL string (NSString) with spaces and & characters. How do I url encode the entire string (including the & ampersand character and spaces)?
189
votes
9
answers
197k
views
Convert NSArray to NSString in Objective-C
I am wondering how to convert an NSArray [@"Apple", @"Pear ", 323, @"Orange"] to a string in Objective-C.
187
votes
31
answers
106k
views
How can I convert my device token (NSData) into an NSString?
I am implementing push notifications. I'd like to save my APNS Token as a String.
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)...
181
votes
9
answers
167k
views
Creating NSData from NSString in Swift
I'm trying to ultimately have an NSMutableURLRequest with a valid HTTPBody, but I can't seem to get my string data (coming from a UITextField) into a usable NSData object.
I've seen this method for ...
161
votes
5
answers
72k
views
How to see if an NSString starts with a certain other string?
I am trying to check to see if a string that I am going to use as URL starts with http. The way I am trying to check right now doesn't seem to be working. Here is my code:
NSMutableString *temp = [[...
158
votes
22
answers
79k
views
Remove all but numbers from NSString
I have an NSString (phone number) with some parenthesis and hyphens as some phone numbers are formatted. How would I remove all characters except numbers from the string?
158
votes
5
answers
157k
views
How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)
In my iOS 5 app, I have an NSString that contains a JSON string. I would like to deserialize that JSON string representation into a native NSDictionary object.
"{\"password\" : \"1234\", \"user\" : ...
150
votes
9
answers
163k
views
How do I convert NSInteger to NSString datatype?
How does one convert NSInteger to the NSString datatype?
I tried the following, where month is an NSInteger:
NSString *inStr = [NSString stringWithFormat:@"%d", [month intValue]];
147
votes
8
answers
74k
views
Replacement for deprecated -sizeWithFont:constrainedToSize:lineBreakMode: in iOS 7?
In iOS 7, the method:
- (CGSize)sizeWithFont:(UIFont *)font
constrainedToSize:(CGSize)size
lineBreakMode:(NSLineBreakMode)lineBreakMode
and the method:
- (CGSize)sizeWithFont:(UIFont ...
142
votes
7
answers
141k
views
Split an NSString to access one particular piece
I have a string like this: @"10/04/2011" and I want to save only the "10" in another string. How can I do that?
142
votes
3
answers
114k
views
Capitalize or change case of an NSString in Objective-C
I was wondering how to capitalize a string found in an object in an NSMutableArray.
An NSArray contains the string 'April' at index 2.
I want this to be changed to 'APRIL'.
Is there something simple ...
141
votes
13
answers
121k
views
Objective-C and Swift URL encoding
I have an NSString like this:
http://www.
But I want to transform it to this:
http%3A%2F%2Fwww.
How can I do this?
141
votes
18
answers
157k
views
Objective-C: Reading a file line by line
What is the appropriate way of dealing with large text files in Objective-C? Let's say I need to read each line separately and want to treat each line as an NSString. What is the most efficient way of ...
137
votes
7
answers
193k
views
How to convert NSNumber to NSString
So I have an NSArray "myArray" with NSNumbers and NSStrings. I need them in another UIView so i go like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)...
136
votes
11
answers
103k
views
Remove all whitespaces from NSString
I've been trying to get rid of the white spaces in an NSString, but none of the methods I've tried worked.
I have "this is a test" and I want to get "thisisatest".
I've used whitespaceCharacterSet, ...
127
votes
5
answers
146k
views
AES Encryption for an NSString on the iPhone
Can anybody point me in the right direction to be able to encrypt a string, returning another string with the encrypted data? (I've been trying with AES256 encryption.) I want to write a method which ...
125
votes
9
answers
102k
views
Objective-C formatting string for boolean?
What formatter is used for boolean values?
EDIT:
Example: NSLog(@" ??", BOOL_VAL);, what is ?? ?
125
votes
7
answers
79k
views
Detecting if an NSString contains...?
How can I detect if a string contains a certain word? For example, I have a string below which reads:
@"Here is my string."
I'd like to know if I can detect a word in the string, such as "is" for ...
122
votes
13
answers
64k
views
Collapse sequences of white space into a single character and trim string
Consider the following example:
" Hello this is a long string! "
I want to convert that to:
"Hello this is a long string!"
114
votes
2
answers
69k
views
Get parts of a NSURL in objective-c
I have an NSString with the value of
http://digg.com/news/business/24hr
How can I get everything before the 3rd level?
http://digg.com/news/
112
votes
6
answers
95k
views
How to convert std::string to NSString?
I am trying to convert a standard std::string into an NSString but I'm not having much luck.
I can convert successfully from an NSString to a std::string with the following code
NSString *realm = @&...
108
votes
22
answers
104k
views
Remove HTML Tags from an NSString on the iPhone
There are a couple of different ways to remove HTML tags from an NSString in Cocoa.
One way is to render the string into an NSAttributedString and then grab the rendered text.
Another way is to use ...
106
votes
3
answers
141k
views
String comparison in Objective-C
I've currently got a webserver set up which I communicate over SOAP with my iPhone app. I am returning a NSString containing a GUID and when I attempt to compare this with another NSString I get some ...
101
votes
10
answers
132k
views
NSString with \n or line break
Does anyone know how to use line breaks in NSString? I need to do something like this -
[NSString stringWithFormat:@"%@,\n%@", mystring1,mystring2];
99
votes
5
answers
45k
views
NSString: isEqual vs. isEqualToString
What is the difference between isEqual: and isEqualToString:?
Why are classes adding isEqualTo* methods (isEqualToArray for NSArray, isEqualToData for NSData, ...) instead of just overriding isEqual: ...
97
votes
6
answers
146k
views
Remove characters from NSString?
NSString *myString = @"A B C D E F G";
I want to remove the spaces, so the new string would be "ABCDEFG".
97
votes
1
answer
46k
views
NSString to CFStringRef and CFStringRef to NSString in ARC?
I am trying to understand the correct way of getting an NSString from a CFStringRef in ARC?
Same for going the opposite direction, CFStringRef to NSString in ARC?
What is the correct way to do this ...
95
votes
19
answers
66k
views
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 ...
94
votes
8
answers
56k
views
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 ...
93
votes
3
answers
46k
views
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"
88
votes
3
answers
101k
views
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?
86
votes
5
answers
142k
views
Converting NSString to NSDictionary / JSON
I have the following data saved as an NSString :
{
Key = ID;
Value = {
Content = 268;
Type = Text;
};
},
{
Key = ContractTemplateId;
Value = {...