[NSString stringByReplacingOccurrencesOfString:#""" withString:#""""]; - iphone

I have an NSString and want to save it in CSV format. Because of this, I am replacing all " with "". I tried the following code but the compiler reports an error for it.
newString = [oldstring stringByReplacingOccurrencesOfString:#""" withString:#""""];
where oldstring is 11th ave, "L&T", Khar. oldstring is set from user input.
How can I end up with newString set to 11th ave, ""L&T"", Khar?
Any help will be appreciated.

You need to escape double quotes (") inside your string:
newString = [oldString stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];

Related

String includin Escape character

In textfield when user enter value with escape character, I want to work this same as user enter but it behaves like a string for \e too.
In more detail.
suppose user have entered \e[3~ but when i print it in console it shows \e[3~. Actually this should print like ¿[3~ so it will work perfect for me.
I have tried by this line and it works.
NSString * str=[textField.text stringByReplacingOccurrencesOfString:#"\\e" withString:#"\e"];
But suppose if user enter value in caps like \E[3~ then its not replacing. so i have tried with like
NSString * str=[textField.text stringByReplacingOccurrencesOfString:#"\\e" withString:#"\e"];
str=[textField.text stringByReplacingOccurrencesOfString:#"\\E" withString:#"\E"];
But this is not working in case while user have entered text in small \e[3~. With this when first line will execute to replace \e it will give me perfect result like ¿[3~ but when it will execute next line it gives me same \e[3~ string.
Please suggest me how can i check for both letters and if i can check for all escaping characters at once.
The error in your code is that the second line takes the original string textField.text
instead of the result from the first line. What you probably meant is:
NSString *str = [textField.text stringByReplacingOccurrencesOfString:#"\\e" withString:#"\e"];
str = [str stringByReplacingOccurrencesOfString:#"\\E" withString:#"\E"];
Alternatively, you can do the replacement in a single step by using the
NSCaseInsensitiveSearch option:
NSString *str = [textField.text stringByReplacingOccurrencesOfString:#"\\e"
withString:#"\e"
options:NSCaseInsensitiveSearch
range:NSMakeRange(0, [textField.text length])];

NSMutableString appendFormat... adds spaces?

When trying to append to an NSMutableString with appendFormat - It adds spaces.
NSM is just an NSMutableString, att_1_variable & att_2_variable is NSString
[NSM appendFormat:#"<tagname att_1=\" %# \" att_2=\" %# \">", att_1_variable, att_2_variable];
The result is:
<tagname myattribute=" ContentOfVariable " title=" ContentOfVariable ">
Before passing in the strings I am doing:
NSString* att_1_variable = [att_1_variable_orginal stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Is there any way around this?
Thanks
Regards
Christian
You're adding the spaces yourself, by including them in the format string. In C the escape sequence for a quotation mark is just \", with no trailing (or leading) space. So you want:
[NSM appendFormat:#"<tagname myattribute=\"%#\" title=\"%#\">",
attributeVariable, titleVariable];
If there are spaces between the quotation marks and the variable contents after that then your input variables are padded with spaces. You can trim those with something like:
NSString *trimmedAttributeVariable = [attributeVariable
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
...
[NSM appendFormat:#"<tagname myattribute=\"%#\" title=\"%#\">",
trimmedAttributeVariable, ...
Which will trim spaces and tabs from both ends.
I presume you want the result to be
<tagname myattribute="ContentOfVariable" title="ContentOfVariable">
In that case, remove the excess spaces that were around the format specifiers as such:
[NSM appendFormat:#"<tagname myattribute=\"%#\" title=\"%#\">", attributeVariable, titleVariable];

Why whitespaceAndNewlineCharacterSet doesn't remove spaces?

This code SHOULD clean phone number, but it doesn't:
NSLog(#"%#", self.textView.text);
// Output +358 40 111 1111
NSString *s = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"%#", s);
// Output +358 40 111 1111
Any ideas what is wrong? Any other ways to remove whitespacish characters from text string (except the hard way)?
Try this
NSCharacterSet *dontWantChar = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *string = [[self.textView.text componentsSeparatedByCharactersInSet:dontWantChar] componentsJoinedByString:#""];
The documentation for stringByTrimmingCharactersInSet says:
Returns a new string made by removing from both ends of the
receiver characters contained in a given character set.
In other words, it only removes the offending characters from before and after the string any valid characters. Any "offending" characters are left in the middle of the string because the trim method doesn't touch that part.
Anyways, there are a few ways to do the thing you're trying to do (and #Narayana's answer is good on this, too... +1 to him/her). My solution would be to set your string s to be a mutable string and then do:
[s replaceOccurrencesOfString: #" " withString: #"" options: NSBackwardsSearch range: NSMakeRange( 0, [s length] )];

Replace two or more spaces to &nbsp,&nbsp

How can I encode convert spaces to &nbsp in objective-c?
I need each space is replaced by &nbsp, except when only one space
Example
in text: AAA BBB C
out text: AAAnbsp;nbsp;nbsp;nbsp;BBB C
thanks
Assuming you're talking about NSStrings -- look at the NSString documentation. There are instance methods for replacing strings of the form stringByReplacing...
Given that you use NSString or NSMutableString:
myString = [myString stringByReplacingOccurrencesOfString:#" " withString:#" "];
myString = [myString stringByReplacingOccurrencesOfString:#" " withString:#" "];
Yes, this needs to be done twice for the case of an odd number of spaces in a row.
If you are dealing with C-strings instead of NSStrings or NSMutableStrings, you do the same but with a function instead of a method. I am not going to write such a function here but know that you have to run it twice. :)

How trim the spaces given in a word for textField

I have a word i.e., P roduct Name in a textfield . we have to trim the space between "P" and "r".
Can you please suggest the solution for this.
Thanks in advance.
There is no real reliable solution for this - there is not enough information provided.
(Unless of course you iterated through a word list, which would be, well, horribly inefficient)
P and r could be separate words for all the computer cares..
This can help you to remove spaces between two words.
NSString *string = #"P roduct Name";
NSString *secondString = [string stringByReplacingOccurrencesOfString:#" " withString:#""];
Output: ProductName
If You want to remove white spaces at start and end of your string
NSString *s = #" Product Name ";
s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Output: 'Product Name'