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.
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 some strings like NAVJYOT COMPLEX, NEAR A ONE SCHOOL, SUBHASH CHOWK , MEMNAGAR, Ahmedabad, Gujarat, India.
I want to convert them so the first character is uppercase and remaining are lowercase, e.g: Navjyot Complex, Near A One School, Subhash Chowk, Memnagar, Ahmedabad, Gujarat, India. So please help me convert those strings.
Thanks in advance.
nsstring has the following method
capitalizedString
it returns:
"A string with the first character from each word in the receiver changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values."
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
use This one
NSString *str1 = #"ron";
NSString *str = [str1 capitalizedString];
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...
This should be simple but it's not working. I am trying to strip single quote marks from an NSString named parms using the following (stripped of non-relevant vars in the format string):
NSString *newVar =[[NSString alloc] initWithFormat:#"%#", [parms stringByReplacingOccurrencesOfString:#"'" withString:#""]];
So if parms contains "Mike's Hat" I would expect that newVar would contain "Mikes Hat". Instead it contains "Mike's Hat".
There must be more to your code than you are proving, but the following works perfectly:
NSString *parms = #"Mike's Hat";
NSString *newVar =[parms stringByReplacingOccurrencesOfString:#"’" withString:#""];
NSLog(#"%#",newVar);
Output: Mikes Hat
There could be a possibility that the character ' may not be the same character in your parms string if the above does not work for you.
Turns out, you are using the wrong character copy/paste this character into your string: ’
Just my two cents on this same problem I had in my code.... When I used the single quote on the keyboard to type ' into my code it didn't work. But I was printing string values to the console. When I copied and pasted the ' character from the console into my code it then worked. What is weird is that I'm using the same key on the keyboard to enter the string into a UITextField so I really don't know why the same key gets turned into something different but that's how I solved it.
Hey all, slight problem when i read in an XML form.
NSXMLParse correctly see's the "£" symbol but its prints out the unicode, \U00a3.
I am just reading it to a string.
[pre_Currency appendString:[self cleanString:string]];
CleanString removes \n - \t and i even added parsing out the unicode and replace it with the Char symbol for the "£".
Oddly enough a NSLog here print a "£" symbol, but when it didEndElement i add it to the dictionary,
[number setObject:[self cleanString:pre_Currency] forKey:#"pre_currency"];
It add it as a unicode Char.
Cant understand why, looking at google theres very little aimed at parsing unicode chars.
I dont know but might be useful to you,if you use the stringByReplacingOccurrencesOfString method
NSString *specialChars=#"YOur string with special characters."
specialChars=[specialChars stringByReplacingOccurrencesOfString:#"\U00a3" withString:#"£"];
Thanks
Yes, it happens for symbols and for other languages fonts in they are not in english, I also got reply here that we will have to decode it as follows:
int c = ... /* your 4 text digit unicode ordinal converted to an integer */
charString = [ NSString stringWithFormat:#"%C", c ];
Original link - How to display Text in Arabic in UIlabel