stringByReplacingOcurrencesOfString with special character objective c - ios

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

Related

how to replace the content of NSMutablestring in objective c

I am new in objective c. I want to replace the content of mutable string I am using code as
NSMutableString *myMutableStringObj = [myMutableStringObj stringByReplacingOccurrencesOfString:#" & " withString:#"And"];
But It Shows me warning
Incomparable pointer types assigning to NSMutableString * from NSString *
Any Suggestion about this. I am using MutableString.
It tells you this function returns NSString *. If you want to clean that warning. Just do like this:
NSMutableString *myMutableStringObj = [[myMutableStringObj stringByReplacingOccurrencesOfString:#" & " withString:#"And"] mutableCopy];
See the method signeture from NSString Class.
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement
this method return NSString object.
So you need to convert result into mutalbe string or catch the result in NSString.
Read the documentation for NSString and NSMutableString. That doesn't only apply to you, but to anyone answering here so far. The answers on this thread have been absolutely depressing.
There are methods that can be used for NSString but also work with NSMutableString which create a new, immutable string based on a previous string. Typically methods starting with "stringBy..." will be creating a new string.
And there are methods, in the NSMutableString interface, which modify the contents of an existing NSMutableString. Like "replaceOccurencesOfString".
Read the documentation and pick what is right for you. A method starting with "stringBy" is not going to modify your mutable string, it's creating a new immutable one. To modify a string, use the methods from the NSMutableString interface. Fortunately the compiler tells you when you use the wrong method.
plz use this code
NSMutableString * str1 = [NSMutableString stringWithString:#"Hello Testing"];
str1 = [[str1 stringByReplacingOccurrencesOfString:#"T" withString:#"B"] mutableCopy];
NSLog(#"%#",str1);

Replacing value with string in objective c

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

Objective C using UItextview/NSstring place holders in long strings

please tell me why I get an: Interface type can not be Statically allocated,
error on the code below and what I can do to be able to use this placeholder in the Json string I am building below. Email is a UITextfield.
NSString *CCEmail = email.text;
NSString *jsonInputString =
#"{\"email\": \" %# \",\"password\": \"iamlearningtocode\"}",CCEmail;
To get the %# in your string replaced with the text in CCEmail you need to call
NSString *jsonInputString = [NSString stringWithFormat:#"{\"email\": \" %# \",\"password\": \"iamlearningtocode\"}", CCEmail];
However with this approach a CCEmail containing " would cause the string to become invalid JSON. I suggest you build your data in a proper NSDictionary and use NSJSONSerialization to convert to string.
You have to use stringWithFormat method of NSString class.
NSString *jsonInputString = [NSString stringWithFormat:#"{\"email\": \" %# \",\"password\": \"iamlearningtocode\"}",CCEmail];

stringWithFormat returning newline

I am adding objects to a mutable array by selecting choice(s) from my table view and viewing them in a text field. When I use stringWithFormat, the line of code is automatically adding in characters.
Example: I choose Bob from my table view
Bob
When I do a NSLog, it is actually appearing as
(
Bob
)
But what is appearing in the text field is
( Bob)
Because there is
(\n Bob\n)
Here is the code that I am using to rid of the parentheses and replace commas with semicolons.
// All Names is the mutable array I am adding information in. tableData is another mutable array with set names in them
[AllNames addObject:tableData[0]];
// If I chose the first option
// NSString
ourForces = [NSString stringWithFormat: AllNames[0]];
// NSString
combinedForces = [ourForces stringByReplacingOccurrencesOfString:#"," withString:#";"];
// NSString
twoCombindForces = [combinedForces stringByReplacingOccurrencesOfString:#"(" withString:#""];
// NSString
UltmateCombinedForces = [twoCombindForces stringByReplacingOccurrencesOfString:#")" withString:#""];
//personnel is the text field
personnel.text = UltmateCombinedForces;
Question now is : What is a less messy path to get
( Bob)
to appear as
Bob
in my text field?
Solution update: After the following lines:
// All Names is the mutable array I am adding information in. tableData is another mutable array with set names in them
[AllNames addObject:tableData[0]];
// If I chose the first option
// NSString
ourForces = [NSString stringWithFormat: AllNames[0]];
Include the following line of code:
personnel.text = [AllNames componentsJoinedByString:#";"];
That got rid of the (\n Bob\n) extra characters that were showing up in the field. Thank you all for you help and wisdom. =)
Use componentsJoinedByString: after filling the AllNames array:
personnel.text = [AllNames componentsJoinedByString:#";"];
OK, I think I understand what you're trying to do now.
You have an array of names...
NSArray *names = #[#"Bob", #"Sam", #"Dave"];
And you want a string of all these names...
#"Bob; Sam; Dave"
You seem to be separating them with a semi colon though? ; Is that correct?
You can do this with...
NSString *nameList = [names componentsJoinedByString:#"; "];
But I'm not entirely sure that this is what you're trying to achieve.
In NSLog the output expression
(
Bob
)
represents an array. To get rid of the parentheses and newlines instead of
NSLog(#"%#", AllNames[0]);
write
NSLog(#"%#", AllNames[0][0]);
… and please use variable names starting with a lowercase letter.
You can use NSCharacter Set to remove items you don't want.. The below might offer a less messy solution.
NSString *originalText = #"( Bob) ";
NSMutableCharacterSet *unwantedChars = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];
[unwantedChars addCharactersInString:#"()"];
NSString *refinedText = [[originalText componentsSeparatedByCharactersInSet:unwantedChars] componentsJoinedByString:#""];

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