My NSString is like this:
NSString *myString =
(
“\n \n 24 K CLUB”,
“\n \n 3 DOLLAR CAFE”,
“\n \n A PEACH OF A PARTY”,
“\n \n A ROYAL AFFAIR CAFE”,
“\n \n AFFAIRS TO REMEMBER CATERERS”,
“\n \n AFRIKAN DELI” )
How to get rid of this new line character and white spaces, so that my new string will be like:
newString:
(
"24 K CLUB”,
"3 DOLLAR CAFE”,
“A PEACH OF A PARTY”,
“A ROYAL AFFAIR CAFE”,
“AFFAIRS TO REMEMBER CATERERS”,
“AFRIKAN DELI”
)
I tried :
myString = [myString stringByReplacingstringByReplacingOccurrencesOfString:#"\n" withString:#""];
myString = [myString stringByReplacingstringByReplacingOccurrencesOfString:#" " withString:#""];
but unsuccessfully..getting error:
[__NSArrayI stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x7062200
How about stringByTrimmingCharactersInSet: method?
By stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet], it can remove both ends of whitespace and newline characters.
Your initial declaration of myString is declaring an NSArray, not a NSString. Your calls to stringByReplacingstringByReplacingOccurrencesOfString: withString: should work on the individual NSStrings. Either iterate the array yourself (see here) to trim the strings, or use makeObjectsPerformSelector: (see here) to handle it.
Related
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];
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:#"\"\""];
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] )];
How can I encode convert spaces to   in objective-c?
I need each space is replaced by  , 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. :)
I have a label that displays inches. I would like to display the number with the inch symbol (") or quotation mark. Can I do this with an nsstring?
Thanks!
Sure, you just need to escape the quotation mark.
NSString *someString = #"This is a quotation mark: \"";
NSLog(#"%#", someString );
Output:
This is a quotation mark: "
You can use Double Quote Escape Sequence here. You need to escape it using a backslash :
NSString *str = #"Hello \"World\"";
NSLog(#"Output : %#",str);
Output : Hello "World"
There are some other Escape Sequences also. Take a look at it :
\b Backspace
\f Form Feed
\n Newline
\t Horizontal Tab
\v Vertical Tab
\\ Backslash
\’ Single Quote
\” Double Quote
\? Question Mark
As use of back slash \" has already mentioned so I am answering different. You can use ASCII Code too.
ASCII Code of " (double quote) is 34.
NSString *str = [NSString stringWithFormat:#"%cThis is a quotation mark: %c", 34, 34];
NSLog(#"%#", str);
And Output is:
"This is a quotation mark: "
Swift 4.0 Version
let str = String(format: "%cThis is a quotation mark: %c", 34, 34)
print(str)
SWIFT
let string = " TEST \" TEST "
println(string)
output in console is - TEST " TEST
Yes, you can include a quotation mark in an NSString literal using the backslash to escape it.
For example, to put the string Quote " Quote in a string literal, you would use this:
#"Quote \" Quote"
A backslash followed by a quotation mark simply inserts the quotation mark into the string.
If the string is a literal string, then you can use the escape character to add a quotation mark inside a string.
NSString *string = #"16\"";
Use the following code for Swift 5, Xcode 10.2
let myText = #"This is a quotation mark: ""#
print(myText)
Output:
This is a quotation mark: "