appending to string? - iphone

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]

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"];

How to combine two strings in Objective-C for an iPhone app

How can I combine "stringURL" and "stringSearch" together?
- (IBAction)search:(id)sender;{
stringURL = #"http://www.websitehere.com/index.php?s=";
stringSearch = search.text;
/* Something such as:
stringURL_ = stringURL + stringSearch */
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:stringURL_]]];
}
Philippe gave a good example.
You can also use plain stringWithFormat: method.
NSString *combined = [NSString stringWithFormat:#"%#%#", stringURL, stringSearch];
This way you can manipulate string even more by putting somethig inbetween the strings like:
NSString *combined = [NSString stringWithFormat:#"%#/someMethod.php?%#", stringURL, stringSearch];
NSString* combinedString = [stringUrl stringByAppendingString: search.text];
NSString * combined = [stringURL stringByAppendingString:stringSearch];
Instead of stringByAppendingString:, you could also use
NSString *combined = [NSString stringWithFormat: #"%#%#",
stringURL, stringSearch];
This is especially interesting/convenient if you have more than one string to append. Otherwise, the stringbyAppendingString: method is probably the better choice.
You can use stringByAppendingString:
stringURL = [#"http://www.websitehere.com/index.php?s="
stringByAppendingString:search.text];
If you want to have some control about the format of the parameter you should assemble
your URL string with
[NSString stringWithFormat:#"http://www.websitehere.com/index.php?s=%#", search.text]
This solution is charming because you can append almost anything which can be inserted into a printf-style format.
I would not have given the answer of such general question.
There are many answers of same type question have already given. First find the answer of your question from existing question.
NSString* myURLString = [NSString stringWithFormat:#"http://www.websitehere.com/index.php?s=%#", search.text];

How to add ' in NSString in ios sdk?

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.

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";