Replacing value with string in objective c - ios

i want to replace the value of placemark.name with the string MyPostition in a textfield station but this lines of code doesn't work any suggestions please
_station.text = [NSString stringWithFormat:#"%#",placemark.name];
_station.text=[NSString stringWithFormat:#"%#",#"MyPosition"];

If MyPosition is a variable from which you want to set the value then please remove the " from this variable as follows:
_station.text=[NSString stringWithFormat:#"%#",MyPosition];
Update as understood by your requirement:
_station.text = [NSString stringWithFormat:#"%#",placemark.locality];

Please try this code. Its may be useful to you
NSString *str = #"This is a MyPosition";
str = [str stringByReplacingOccurrencesOfString:#"MyPosition"
withString:#"duck"];

Use this format:
_station.text = [NSString stringWithFormat:#"%#",placemark.locality];

Related

stringByReplacingOcurrencesOfString with special character objective c

I am trying to replace instances of 's with s or alternatively instances of s with 's. However, the result of the code below is an empty string. What could I be doing wrong?
NSString *myStr = #"Eat at Joe's";
NSString *newStr = [myStr stringByReplacingOccurrencesOfString:#"\'s" withString:#"s"];
//edited as per Vadian
NSLog(#"newStr:%#",newStr); //logs as newStr:
You forgot the placeholder
NSLog(#"newStr: %#", newStr);
Don't you get the warning
Data argument not used by format string

Replace multiple placeholders in string

What I am looking to do is use placeholders in a string and replace the placeholders with data specific to the user. I can setup the placeholders to be anything so basically I am looking to do the following:
Setup placeholders in a string (up to 4 placeholders)
Replace those placeholders with strings I specify
Here is what I have. I currently have a url that has a set of placeholders like so. http://example.com/resource?placeholder1=placeholder2 or http://placeholder1:placeholder2#example.com/something?placeholder3
How do I properly label the placeholders and replace them?
Thank you in advance for any help.
You can use the stringByReplacingOccurrencesOfString method as below
NSString *strUrl = #"http://example.com/resource?placeholder1=placeholder2";
strUrl = [strUrl stringByReplacingOccurrencesOfString:#"placeholder1" withString:#"value1"];
strUrl = [strUrl stringByReplacingOccurrencesOfString:#"placeholder2" withString:#"key1"];
Quote : "I want to replace placeholder1 with an NSString I have already created called value1 and placeholder2 with an NSString called key1."
NSString *mainUrl = "http://example.com/resource";
NSString *string1 = "value1";
NSString *string2 = "value2";
Now change your URL:
NSString *newURL = [NSString NSStringWithFormat:#"%#?%#=%#",mainUrl,string1,string2];
This will generate newURL : http://example.com/resource?value1=value2
This might help you.
#define placeHolder1 #"<>p1p1p1<>"
#define placeHolder2 #"<>p2p2p2<>"
And place this in a function of yours where you want to replace strings
NSString * string = [NSString stringWithFormat:#"http://example.com/resource?%#=%#",placeHolder1,placeHolder2];
NSLog(#"string %#",string);
NSString * replacerForP1 = #"123";
NSString * replacerForP2 = #"741";
string = [string stringByReplacingOccurrencesOfString:placeHolder1
withString:replacerForP1];
string = [string stringByReplacingOccurrencesOfString:placeHolder2
withString:replacerForP2];
NSLog(#"string %#",string);
It would be best to keep your placeholder strings in the constants defined somewhere. And ofcourse the replacement of those placeholders will be dynamic as you said so cannot make them constants.
Tell me if this helps or if you require further assistance in the matter.

whats the correct way of fix label text in xcode?

This is my code:
[_HintLabel setText:[NSString stringWithFormat:#"%#", [lst_word objectAtIndex:nCurrentWord]]];
[_HintLabel.stringByPaddingToLength:prlt.length withString: #"~" startingAtIndex:0];
But the second line of code doesnt work,
How do I fix it so it works?
I believe you want this:
NSString *newText = [_HintLabel.text stringByPaddingToLength:prlt.length withString: #"~" startingAtIndex:0];
_HintLabel.text = newText;
BTW - the 1st line in your question doesn't need the stringWithFormat: if the object in the array is already a string. Just do:
_HintLabel.text = lst_word[nCurrentWord];

How can I concatenate the string that I'm using to define this label text

this seems like it is probably dead simple to somebody but I'm stuck.
I have this little app that picks a random thing from an array of things and then displays it to the user in the form of a question.
NSLog(#"How about %#?", theThing);
// How About...
self.theThingLabel.text = theThing;
//How do I add a question mark to the end of that string
in the code above the NSLog string works just like I want the label to work. I take care of the "how about" part as a string above the displayed result, but I can't figure out how to add that question mark .. something like theThing+"?"
I tried a bunch of stuff but instead of getting lucky I got warned by xcode over and over.
Try this
self.theThingLabel.text = [theThing stringByAppendingString:#" ?"];
self.theThingLabel.text = [NSString stringWithFormat:#"How about %#?",theThing];
This is very easy
Use this :
self.theThingLabel.text = [NSString stringWithFormat:#"How about %# ?", theThing];
theThing = [NSString stringWithFormat:#"How about %#?",theThing];
NSLog(#"%#", theThing);
then
self.theThingLabel.text = theThing;
Hope it helps..
NSString *what= #"What";
NSString *finalString = [NSString stringWithFormat:#"How about %# ..?", what];
NSLog(#"%#", finalString);
Hope that helps

iOS finding string within a string

Hello everyone I am trying find a string inside a string
lets say I have a string:
word1/word2/word3
I want to find the word from the end of the string to the last "/"
so what I will get from that string is:
Word3
How do I do that?
Thanks!
You are looking for the componentsSeparatedByString: method
NSString *originalString = #"word1/word2/word3";
NSArray *separatedArray = [originalString componentsSeparatedByString:#"/"];
NSString *lastObject = [separatedArray lastObject]; //word3
once check this one By using this one you'l get last pathcomponent values,
NSString* theFileName = #"how /are / you ";
NSString *str1=[theFileName lastPathComponent];
NSLog(#"%#",str1);
By using lastPathComponent you'l get the last path component directly no need to take array for separate the string.
you must use NSScanner class to split substring.
check this.
Objective C: How to extract part of a String (e.g. start with '#')
NSString *string = #"word1/word2/word3"
NSArray *arr = [string componentsSeperatedByString:#"/"];
NSSting *str = [arr lastObject];
You can find it also with this way:
NSMutableString *string=[NSMutableString stringWithString:#"word1/word2/word3"];
NSRange range=[string rangeOfString:#"/" options:NSBackwardsSearch];
NSString *subString=[string substringFromIndex:range.location+1];
NSRegularExpression or NSString rangeOfString:options:range:locale: (with options to search backwards).
The answer really depends on exactly what the input string will contain (how consistent it is).

Resources