How to add ' in NSString in ios sdk? - iphone

I have a NSString like this
NSString *mystring = #"RahulVyas";
Now I want to add this ' so the new string would be 'RahulVyas'
How to do this ?

How about:
NSString *newString = [NSString stringWithFormat:#"'%#'", mystring];

You just want to surround the string with single quotes, right? You can just use stringWithFormat:
mystring = [NSString stringWithFormat:#"'%#'", mystring];

NSString *mystring=#"'RahulVyas'";
NSLog(#"%#", mystring);
Seems to work just fine for me.

Related

how to replace %# with a nsstring variable? iPhone

How to replace the following correctly?
NSString *stringURL = [NSString stringWithFormat:#"http://192.168.1.183:8001/GetDocument.aspx?id=%# & user=admin_document",self.index];
NSURL *targetURL = [NSURL URLWithString:stringURL];
I want to replace %# with self.index.
Your problem is more likely the whitespace. Try removing it:
NSString *stringURL = [NSString stringWithFormat:#"http://192.168.1.183:8001/GetDocument.aspx?id=%#&user=admin_document", self.index];
NSLog(#"URL:%#", stringURL); //You can print out to check
NSURL *targetURL = [NSURL URLWithString:stringURL];
Remove the whitespaces in the link.
If self.index is not an NSString object, then the %# format specifier will not work. I suspect with a name like index it is probably an NSInteger or similar, in which case you want to use %d instead.
Have a look at this Apple documentation for all the possible format specifiers and the types they correspond to. The compiler should pick you up on this though and suggest the correct specifier.
just parse and use it if it is integer then you use like this
int a=10;
NSString *stringURL = [NSString stringWithFormat:#"this is my number %d",a];
and if you want to use string then you did like this
NSString stringName=#"john";
NSString *stringURL = [NSString stringWithFormat:#"this is my name %#",stringName];

NSString like '\uf003" replace '\' with some other string in in obj c

i need to send smiley to other user through iphone app ,so i need to replace \ string with some unique string in obj c.
here if your string is #"\ud83d\ude04" then it is give error "Invalid Character" so put this ' special character and then use it ..
NSString *str = #"\'ud83d\'ude04";//// here if your string is #"\ud83d\ude04" then it is give error "Invalid Character" so put this ' special character and then use it
NSString *smileWithString = [str stringByReplacingOccurrencesOfString:#"\'" withString:#":)"];
[smileWithString retain];
NSLog(#"\n\n SmileString %# Str %#",smileWithString);
Update:
Here’s how to convert NSString to NSData – it’s really simple:
NSString *myString = #"Some String";
NSData *myData = [myString dataUsingEncoding:NSUTF8StringEncoding];
And what about the reverse conversion, i.e. how to convert NSData to NSString? Here’s one quick way:
NSString *myString = [NSString stringWithFormat:#"%.*s",[myData length], [myData bytes]];
Use encoding of NSString and when need to use or show string decode it.
Refer base64-encoding link.
Your looking for stringByReplacingOccurrencesOfString that should do the trick.
NSString *newString = [oldString stringByReplacingOccurrencesOfString:#"\" withString:#"uniqueString"];

NSString stringWithFormat adding a newline by itself

Why does this:
NSString *url = [NSString stringWithFormat:#"http://www.example.com/profile/%#/?s_iphone=true", author];
NSLog(#"url: %#", url);
Output this:
http://www.example.com/profile/AuthorName
/?s_iphone=true
Needless to say the url won't load b/c there is a newline added to the string by itself. I've tried removing whitesapces/newlines and still had the same output. Its driving me crazy.
Matt
Try this -
NSString *url = [NSString stringWithFormat:#"http://www.example.com/profile/%#/?s_iphone=true", [author stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSLog(#"url: %#", url);
It will fix any new line characters at the beginning and end of your author string.
Author definitely has a new line in it. Try this:
NSLog(#"The author is %# on a new line?", author);

appending to string?

I need to append my string in one line of code.
Is there any stringByAppendingStrings?
NSString *url=[url stringByAppendingString:changeString];
url=[url stringByAppendingString:#"&latitude=52.3328117&longitude=4.878836"];
url=[url stringByAppendingString:#"time=2:00"];
Use NSMutableString to get the work done.
Check out the documentation for more information to edit any Mutable String
You could use stringWithFormat if you're creating the string all at once.
NSString *url = [NSString stringWithFormat:#"%#%#%#", string1, string2, string3];
you could try:
url = [NSString stringWithFormat: #"%#%#&latitude=52.3328117&longitude=4.878836time=2:00" , url, changeString]

How to concatenate two strings on iPhone?

How to connect string "Hello" and string "World" to "HelloWorld"? Looks like "+" doesn't work.
NSString *string = [NSString stringWithFormat:#"%#%#", #"Hello", #"World"];
NSLog(#"%#", string);
That should do the trick, although I am sure there is a better way to do this, just out of memory. I also must say this is untested so forgive me. Best thing is to find the stringWithFormat documentation for NSString.
How about:
NSString *hello = #"Hello";
NSString *world = #"World";
NSString *helloWorld = [hello stringByAppendingString:world];
If you have two literal strings, you can simply code:
NSString * myString = #"Hello" #"World";
This is a useful technique to break up long literal strings within your code.
However, this will not work with string variables, where you'd want to use stringWithFormat: or stringByAppendingString:, as mentioned in the other responses.
there's always NSMutableString..
NSMutableString *myString = [NSMutableString stringWithString:#"Hello"];
[myString appendString: #"World"];
Note:
NSMutableString *myString = #"Hello"; // won't work, literal strings aren't mutable
t3.text=[t1.text stringByAppendingString:t2.text];
Bill, I like yout simple solution and I'd like to note that you can also eliminate the space between the two NSStrings:
NSString * myString = #"Hello"#"World";