This question already has answers here:
String replacement in Objective-C
(6 answers)
Closed 8 years ago.
I have a NSString:
NSString *string1 = #"http://example.com/this-is-an-example-post"
By removing a range of characteres I have substringed it to:
NSString *string2 = #"this-is-an-example-post"
But I am struggling to get it into the form of:
NSString *string3 = #"this is an example post"
Any help? Much appreciated.
For example this way
string3 = [string2 stringByReplacingOccurrencesOfString:#"-" withString:#" "];
Related
This question already has answers here:
ios UTF8 encoding from nsstring
(5 answers)
Closed 6 years ago.
How to convert NSString to UTF8Encode ?
My NSLog print the same result as input string
NSString* username =#"MYUSERNAME";
username=[username stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* Password =#"MYUSERNAMEPASS";
Password=[Password stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"username=%#",username);
NSLog(#"Password=%#",Password);
You need to use stringByAddingPercentEscapesUsingEncoding instead of stringByReplacingPercentEscapesUsingEncoding.
NSString *encodeUserName = [username stringByAddingPercentEscapesUsingEncoding:NSUTF32StringEncoding];
Also stringByAddingPercentEscapesUsingEncoding is deprecated so batter to use stringByAddingPercentEncodingWithAllowedCharacters like this
NSString *encodeUserName = [username stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
This question already has answers here:
Objective-C Split()?
(5 answers)
Closed 8 years ago.
I have received below string response
USA : United states of america
i need to get before ":" data's. Below I have posted sample what i need?
USA
Hi i think you need to check if the ":" char is exists first because it will crash.
here is example for how to do it:
NSString *yourString = #"USA : bla bla bal";
NSRange range = [yourString rangeOfString:#":"];
if(range.location != NSNotFound) // string contains
{
int indexOf = range.location;
NSString *prefix = [yourString substringWithRange:NSMakeRange(0, indexOf)];
// prefix = "USA "
}
if you want you can trim the prefix string and remove all the empty spaces like this
NSString *trimmedString = [prefix stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
Try like this:-
NSString*yourStr=#"USA : United states of america";
NSString *str=[yourStr componentsSeparatedByString:#":"][0];
NSLog(#"%#",str);
This question already has answers here:
How to initialize NSString to NSMutableString?
(4 answers)
Closed 8 years ago.
Is there any way to convert an NSString to NSMutableString in Objective-C?
NSString *string = #"A string.";
NSMutableString *mutableString = [string mutableCopy];
This question already has answers here:
How to convert int to NSString?
(4 answers)
Closed 8 years ago.
Good morning,
First of all excuse me for my english.
A partner has made an app that place an image in the ios display depending the placement id you write on a textfield. He defined the textfield and the variable as a integer, but when he finished he realized that it should be an string. Now its all coded for a int, is there an easy way to parse it to string?
See this other SO question: How can I convert an int to an NSString?
In short, with the code NSString *string = [NSString stringWithFormat:#"%d", theinteger];
int yourIntVar = ??;
NSString *intIntoString = [NSString stringWithFormat:#"%d", yourIntVar];
use this:
myTextfield.text = [NSString stringWithFormat:#"%d",myInt];
int num = 2;
NSString *yourString = [NSString stringWithFormat:#"%d",num];
This question already has answers here:
Is it possible to include a quotation mark as part of an nsstring?
(7 answers)
Closed 8 years ago.
i want to initialize NSString is as "hai" with double qoutes..is it possible? any help please?
but NSString *str = #"hai"; i want to convert it as "hai" without direct initialization?any help pls?
If you want to convert an existing string please use this:
NSString *newString = [NSString stringWithFormat:#"\"%#\"",str];
NSString* foo = #"\"hai\"";