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);
Related
In any URL, you can have special characters like *? & ~ : / *
and soon if not already, accentuated characters
What I'd like is to convert ANY url into it's nearest equivalent in pure ASCII character
THEN replacing any remaining spécial charaters by a _
I've tried this looking and inspiring myslef with many examples over the net, but it do not work (for example, using this code, the character "é" is not converted to "e" in #"http://www.mélange.fr/~fermer.php?aa=10&ee=13")
NSMutableCharacterSet *charactersToKeep = [NSMutableCharacterSet alphanumericCharacterSet];
[charactersToKeep addCharactersInString:#"://&=~?"];
NSCharacterSet* charactersToRemove = [charactersToKeep invertedSet];
myNSString = [[[myNSString decomposedStringWithCanonicalMapping] componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:#""];
to start, after I will have to convert remaining special characters with _
How may I achieve this ?
As an example (and only for example), I'd like to convert :
http://www.mélange.fr/~fermer.php?aa=10&ee=13
to
http___www.melange.fr__fermer_php_aa_10_ee_13
of course without having to check one by one each possible special or accentued character.
Two thoughts:
To replace accented characters with unaccented ones, there are a couple of candidates:
You can use CFStringTransform:
NSMutableString *mutableString = [string mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)mutableString, NULL, kCFStringTransformStripCombiningMarks, NO);
You could use dataUsingEncoding:allowLossyConversion:
NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Characters it doesn't know what to do with become ? and but this sometimes replaces one character with multiple characters (e.g. © with (C)), which you may or may not want.
Once you do this international character conversion, it looks like you want replace any non-alphanumeric character (or period) with an underscore, which you could do with a stringByReplacingOccurrencesOfString with a regular expression:
NSString *result = [string stringByReplacingOccurrencesOfString:#"[^a-z0-9\\.]"
withString:#"_"
options:NSRegularExpressionSearch | NSCaseInsensitiveSearch
range:NSMakeRange(0, [string length])];
There are lots of permutations of this regular expression that will accomplish the same thing, but hopefully you get the idea.
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];
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] )];
I know it must be a very simple thing to do but I've never had to treat strings before (in Objective-C) and apparently there's not RegEx on Cocoa-Touch.
Well, the situation is:
I have a text field to get a value (money, such as 32.10 for instance).
The problem:
If the user types in a symbol such as #, /, # etc. my app will crash.
The Question: How can I treat this string to remove the symbols if there are any?
you can try this:
NSString *s = #"12.827##584";
NSCharacterSet *removeCharSet = [NSCharacterSet characterSetWithCharactersInString:#"/:##"];
s = [[s componentsSeparatedByCharactersInSet: removeCharSet] componentsJoinedByString: #""];
NSLog(#"%#", s);
You do get regex in Cocoa Touch.
Here's a good discussion of the varying degrees of regex power in iOS, the blocks example at the end should get you most of the way there.
http://volonbolon.net/post/861427732/text-handling-in-ios-4
I understand you're trying to figure out the number included in the UITextFields's text property and assign it to a float variable.
Try using an NSScanner for this:
NSScanner* textScanner = [NSScanner localizedScannerWithString:textfield.text];
float* floatValue;
[textScanner scanFloat:&floatValue];
floatValue now contains the parsed float value of your textfield.
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'?