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];
Related
I'm a noob to iphone development and I am having a very weird issue with xcode. For some reason, it is randomly adding trailing whitespace to the end of my strings. My initial string value is parsed from an xml and then from there I attempt grab the last section of the string. This is proving to be difficult because of this random whitespace. Any help is greatly appreciated.
MY CODE
//initial String
<title>March 15 2013 Metals Commentary: Thomas Vitiello</title>
//Parsing
NSString *name = [[stories objectAtIndex: storyIndex] objectForKey: #"title"];
NSLog(#"myname: %# %i", name, name.length);
//Log at this point
myname: March 15 PM Metals Commentary: Thomas Vitiello
59
//Attempt at Removing Whitespace
NSArray *lastName=[name componentsSeparatedByString:#" "];
name = [[lastName objectAtIndex:6]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(#"myname: %# %i", name, name.length);
//Log at this point
myname: Vitiello
9 //<--Should be 8, not 9. This annoying whitespace is also particularly long, taking more than 1 character space, despite obviously being 1 character long.
You could try to remove the newline also from the string. Your string contains not only white spaces but also new-line.
NSString *trimmmedContent = [contents stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Note that you probably don't have <title>March 15 2013 Metals Commentary: Thomas Vitiello</title>. More likely you have:
<title>March 15 2013 Metals Commentary: Thomas Vitiello
</title>
Unless you use an option to prevent it, the XML parser will generally include the whitespace between tags, so you get that trailing newline.
You're using whiteSpaceCharacterSet, which is described like this:
Returns a character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).
But from your own question, it appears that there's a newline in your string. Notice how the length appears on a different line from the string, even though there's no newline in your format string. A newline isn't one of the characters that you're trimming. To solve the problem, use whitespaceAndNewlineCharacterSet instead.
Can any one guide me how to escape the special characters in iphone development,currently when i use this characters it gets me junk value,
The Following are the charaters :
""$$¢£฿¥₡€₭,
Thanks in advance
You can specify the special characters set you want to ignore in character set.
NSString *oldString = #"dsfas$$$///dfa****";
NSCharacterSet *theCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"+-*!%$/_"];
NSString *newString = [[oldString componentsSeparatedByCharactersInSet:theCharacterSet] componentsJoinedByString:#""];
NSLog(#"newString: %#",newString);
I was wondering how to add whitespaces inbetween letters/numbers in a string with Objective-C.
I have the sample code kinda working at the moment. Basically I want to turn "West4thStreet" into "West 4th Street".
NSString *myText2 = #"West4thStreet";
NSString *regexString2 = #"([a-z.-][^a-z .-])";
for(NSString *match2 in [myText2 componentsMatchedByRegex:regexString2 capture:1L]) {
NSString *myString = [myText2 stringByReplacingOccurrencesOfString:match2 withString:#" "];
NSLog(#"Prints out: %#",myString); // Prints out: Wes thStreet // Prints out: West4t treet
}
So in this example, it's replacing what I found in regEx (the "t4" and "hS") with spaces. But I just want to add a space inbetween the letters to separate out the words.
Thanks!
If you wrap parts of your regex patterns in parentheses, you can refer to them as $1, $2, etc in your replacement string (patterns are numbered from left to right, by the order of their opening parenthesis).
NSString *origString = #"West4thStreet";
NSString *newString = [origString stringByReplacingOccurrencesOfRegex:#"(4th)" withString:#" $1 "];
Not sure I understand your broader use case, but that should at least get you going...
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. :)
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.