NSString removing single quote in string - iphone

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.

Related

How to use regular expression in iPhone app to separate string by , (comma)

I have to read .csv file which has three columns. While parsing the .csv file, I get the string in this format Christopher Bass,\"Cry the Beloved Country Final Essay\",cbass#cgs.k12.va.us. I want to store the values of three columns in an Array, so I used componentSeparatedByString:#"," method! It is successfully returning me the array with three components:
Christopher Bass
Cry the Beloved Country Final Essay
cbass#cgs.k12.va.us
but when there is already a comma in the column value, like this
Christopher Bass,\"Cry, the Beloved Country Final Essay\",cbass#cgs.k12.va.us
it separates the string in four components because there is a ,(comma) after the Cry:
Christopher Bass
Cry
the Beloved Country Final Essay
cbass#cgs.k12.va.us
so, How can I handle this by using regular expression. I have "RegexKitLite" classes but which regular expression should I use. Please help!
Thanks-
Any regular expression would probably turn out with the same problem, what you need is to sanitize your entries or strings, either by escaping your commas or by highlighting strings this way: "My string". Otherwise you will have the same problem. Good luck.
For your example you would probably need to do something like:
\"Christopher Bass\",\"Cry\, the Beloved Country Final Essay\",\"cbass#cgs.k12.va.us\"
That way you could use a regexp or even the same method from the NSString class.
Not related at all, but the importance of sanitizing strings: http://xkcd.com/327/ hehehe.
How about this:
componentsSeparatedByRegex:#",\\\"|\\\","
This should split your string whereever " and , appear together in either order, resulting in a three-member array. This of course assumes that the second element in the string is always enclosed in parentheses, and the characters " and , never appear consecutively within the three components.
If either of these assumptions is incorrect, other methods to identify string components may be used, but it should be made clear that no generic solution exists. If the three component strings can contain " and , anywhere, not even a limited solution is possible in such cases:
Doe, John,\"\"Why Unescaped Strings Suck\", And Other Development Horror Stories\",Doe, John <john.doe#dev.null>
Hopefully there is nothing like the above in your CSV data. If there is, the data is basically unusable, and you should look into a better CSV exporter.
The regex you're searching for is: \\"(.*)\\"[ ^,]*|([^,]*),
in ObjC: (('\"' && string_1 && '\"' && 0-n spaces) || string_2 except comma) && comma
NSString *str = #"Christopher Bass,\"Cry, the Beloved Country ,Final Essay\",cbass#cgs.k12.va.us,som";
NSString *regEx = #"\\\"(.*)\\\"[ ^,]*|([^,]*),";
NSMutableArray *split = [[str componentsSeparatedByRegex:regEx] mutableCopy];
[split removeObject:#""]; // because it will print always both groups even if the other is empty
NSLog(#"%#", split);
// OUTPUT:
2012-02-07 17:42:18.778 tmpapp[92170:c03] (
"Christopher Bass",
"Cry, the Beloved Country ,Final Essay",
"cbass#cgs.k12.va.us",
som
)
RegexKitLite will add both strings to the array, therefore you will end up with empty objects for your array. removeObject:#"" will delete those but if you need to maintain true empty values (eg. your source has val,,ue) you have to modify the code to the following:
str = [str stringByReplacingOccurrencesOfRegex:regEx withString:#"$1$2∏"];
NSArray *split = [str componentsSeparatedByString:#"∏"];
$1 and $2 are those two strings mentioned above, ∏ is in this case a character which will most likely never appear in normal text (and is easy to remember: option-shift-p).
The last part looks like it will never contain a comma. Neither will the first one as far as I can see...
What about splitting the string like this:
NSArray *splitArr = [str componentsSeparatedByString:#","];
NSString *nameStr = [splitArr objectAtIndex:0];
NSString *emailStr = [splitArr lastObject];
NSString *contentStr = #"";
for(int i=1; i<[splitArr count]-1; ++i) {
contentStr = [contentStr stringByAppendingString:[splitArr objectAtIndex:i]];
}
This will use the first and last string as is, and combine the rest into the content.
Kind of a hack, but a name and an email address will never contain a comma, right?
Is the title guarantied to have the quotation marks? And is it the only component that can have them? Because then componentSeparatedByString:#"\"" should get you this:
Christopher Bass,
Cry, the Beloved Country Final Essay
,cbass#cgs.k12.va.us
Then use componentSeparatedByString:#"," or substringFrom/ToIndex: to get rid of the two commas in the first and last component.
Here's a solution using substring:
NSString* input = #"Christopher Bass,\"Cry, the Beloved Country Final Essay\",cbass#cgs.k12.va.us";
NSArray* split = [input componentsSeparatedByString:#"\""];
NSString* part1 = [split objectAtIndex:0];
NSString* part2 = [split objectAtIndex:1];
NSString* part3 = [split objectAtIndex:2];
part1 = [part1 substringToIndex:[part1 length] - 1];
part3 = [part3 substringFromIndex:1];
NSLog(part1);
NSLog(part2);
NSLog(part3);

NSString/NSMutableString strange behaviour

OK here is nsmutablestring
data = [NSMutableString stringWithFormat:#"&cb_games%5B%5D="];
Now when ever I try to print or use this string I get big number instead of %5B and %5D not sure why this is happeing any help would be apritiated
thanks
The reason you get unexpected output is that '%' is used as conversion specifier in printf and obviously NSLog and NSString formattings. You need to escape '%' if you don't want it to be interpreted as a conversion specifier. You can escape '%' by preceding it with another '%' like '%%'.
Your string should look like,
#"&cb_games%%5B%%5D="
And the #August Lilleaas's answer is also noteworthy.
Try this:
NSString * data = [NSMutableString stringWithFormat:#"&cb_games%%5B%%5D="];
NSLog(#"%#",data);
stringWithFormat is basically printf, and it attempts to replace your percentages with values that you haven't provided, which is why wierd stuff happens.
[NSMutableString stringWithFormat:#"Hello: %d", 123];
// #"Hello: 123"
If you want a mutable string from a string, try this:
[NSMutableString stringWithString:#"Abc %2 %3"];
// #"Abc %2 %3"
The % is used for string formatting and stuff. I imagine you need to escape the character or something, possibly with a slash.
Did you mean to write?
data = [NSMutableString stringWithString#"&cb_games%5B%5D="]

NSXMLParser and the "£" symbol

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

iPhone dev: Replace uppercase characters in NSString with space and downcase

I have:
NSString *promise = #"thereAreOtherWorldsThanThese";
which I'm trying to transform into the string:
#"There are other worlds than these"
I'm guessing this is a regex job, but I'm very new to Objective C, and have so far had no luck. I would greatly appreciate any help!
I'd use GTMRegex (http://code.google.com/p/google-toolbox-for-mac/), for example:
NSString *promise = #"thereAreOtherWorldsThanThese";
GTMRegex *regex = [GTMRegex regexWithPattern:#"([A-Z])"];
NSLog(#"%#", [[regex stringByReplacingMatchesInString:promise
withReplacement:#" \\1"] lowercaseString]);
As for removing the uppercase letters you can simply use lowercaseString on NSString.
But as for inserting spaces just before an uppercase letter, I would agree that it would be a job for a regex, and sadly, my regex fu is rubbish :)
Without using any libraries you can use this NSString category I posted. Just perform lowerCaseString on the string array.
How do I convert an NSString from CamelCase to TitleCase, 'playerName' into 'Player Name'?

Replace occurrences of NSString - iPhone

I have a long NSString in which I m trying to replace special characters. Part of my string looks like this:
"veau (c\u00f4telette)","veau (filet)","agneau (gigot)","agneau (c\u00f4telette)","b**\u0153**uf (hach\u00e9)","porc (hach\u00e9)"
I would like to replace all the \u0153 with "oe". I've tried:
[response stringByReplacingOccurrencesOfString:#"\u0153" withString:#"oe"];
but it doesn't work.... I don't understand why!
The backslash is an escape character, so if you want to specify the actual backslash character in a string literal, you need to use two backslashes.
NSString *new = [old stringByReplacingOccurrencesOfString: #"\\u0153" withString:#"oe"];
NSString is immutable, so the function generates a new string that you have to store:
NSString *new = [old stringByReplacingOccurrencesOfString:#"\u0153" withString:#"oe"];