Weird \U0000202a and \U0000202c characters? [duplicate] - ios

This question already has answers here:
remove unwanted characters in iOS
(4 answers)
Closed 6 years ago.
I bumped into an interesting situation while I am writing an application about contact list.
The weird situation is that after I successfully managed to retrieve contacts from the iPhone's list, some phone numbers listed as;
#"\U0000202a xxx xxx xxxx\U0000202c".
When I try to use;
modifiedPN = [modifiedPN stringByReplacingOccurrencesOfString:#" " withString:#""];
and
modifiedPN = [modifiedPN stringByReplacingOccurrencesOfString:#"\\U0000202" withString:#""];
nothing changes.
I also tried
[mPN containsString:#" "]
and
[mPN containsString:#"\\U0000202"]
but they both returns "NO"
I think this problem occurs when user was saved from Whatsapp.
Does anyone know how to fix this issue?
I only want the phone number without spaces or weird \U0000202 charachter.

Go the other way and extract the number:
NSString *numString = #"\U0000202a 123 456 7890\U0000202c";
NSString *extractedString = [[numString componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:#""];
// extractedString = 1234567890
This also works with (123) 456 7890

Related

Remove Space After a Particular Character

I have an string which is coming from a server :
<p>(555) 555-5555 </p>
I want to remove any space after teland up to 10 characters.
For removing only spaces between characters you can use this
NSString *strNum = #"(555) 555-5555";
strNum = [strNum stringByReplacingOccurrencesOfString:#" " withString:#""];
Hope this will help you..!! :)
Removing spaces is called Trimming. You can find a possible solution here, or here
Solution copied here in case links break :
NSString *string = #" this text has spaces before and after ";
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
This is slightly better than replacing " " with "", because it uses the charset, and you "never know how white spaces are gonna be in other character sets". The OS does know, so better trust it.
Since your question isn't 100% clear, I'm assuming that is what you need, but feel free to comment for more help :)
EDIT : I might have misunderstood you. If you need to remove all spaces in the string, you could just use Abha's answer.
EDIT 2 : Okay we're about to solve this outstanding mystery.
You want to trim ALL spaces after telinside your string.
What you need to do (and for the sake of learning I'm not gonna write code) is :
Find (using available NSString methods) the word telinside the string. Once you found it, you can find it's index inside the string (after all, a string is just an array of char).
Once you have the index, you just have to use Abha's answer (replace occurences of " " with "") in the range starting with the index you found and ending at that index + 10 (or whatever number you need).
It should be between 2 to 5 lines long, using various NSString methods or, if you really want to, a loop.
Answers you should check for inspiration :
Find string in string
Replace characters in range
Find index of char in string
Though, for the sake of conversation, I'm assuming you only need the phone number (not the tel). So removing ALL spaces should be enough (again, Abha's answer). I don't see any reason why you would take particular care for the first portion of the string when you probably won't use it anyway. Maybe I'm wrong but, you're saying you're new and I'm thinking you're approaching this the wrong way.
Also, to add something else, if you have any control over the server, the server itself should not send tel:(555) 555 5555. That's prone to mistakes. Either the server sends a string to be displayed, with proper characters and nice writing, like Telephone : (555) 555 5555", or you receive ONLY the phone number in a phone object (json or something), like 5555555555. If you have any control over the server, make it send the correct information instead of sending something not practical and having to rework it again.
Note that usually, it's the second option. The server sends something raw, and you just modify it to look good if necessary. Not the other way around.
You need to use NSMutableString class and its manipulation functions provided itself.
NSMutableString *muString = [NSMutableString stringWithString:#"(555) 555-5555"];
[muString insertString:#"" atIndex:10];
Some methods to split your string :
substringFromIndex:
substringWithRange:
substringToIndex:
Edit:
If your string is dynamic and you really dont know the index from where you need to remove extra spaces use below method of NSString class:
stringByReplacingOccurrencesOfString: Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.
Example:
NSString *yourStr = #"(555) 555-5555"; // needs to remove spaces and all
yourStr = [yourStr stringByReplacingOccurrencesOfString:#" " withString:#""]; // after manipulation
Or if you really need to do some more advance changes into your string use NSMutableString class see detail below and example above on the top:
NSMutableString: The NSMutableString class declares the programmatic interface to an object that manages a mutable string—that is, a string whose contents can be edited—that conceptually represents an array of Unicode characters. To construct and manage an immutable string—or a string that cannot be changed after it has been created—use an object of the NSString class.
So you want to remove space from first 10 character, which you think is a tel number, right?
So make a sub string and replace space from that.
NSString *str = #"(555) 555-5555";
NSString *firstPart = [[str substringToIndex:10] stringByReplacingOccurrencesOfString:#" " withString:#""];
then take the second part and merge it.
NSString *secondPart = [str substringFromIndex:10];
NSMutableString *finalString = [NSMutableString stringWithFormat:#"%#%#",firstPart,secondPart];
Is this what you want to do? I've written in step by step for better understanding.
This solution in not generic but may work on your condition
NSString * serverString;
NSArray* stringComp = [serverString componentsSeparatedByString:#"\\"];
NSString* stringOfTel = [stringComp objectAtIndex:1];
NSString* withOutSpaceTel = [stringOfTel stringByReplacingOccurrencesOfString:#" " withString:#""];
serverString = [serverString stringByReplacingOccurrencesOfString:stringOfTel withString:withOutSpaceTel];
and your will get your serverstring with space you want.
Again this may work only for your current solution.
NSString *string = #"(555) 555-5555" //here you need to assign string which you are getting from server
NSString *resultString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
resultString will be the string with no space at start and end of the string.
Hope this helps!

how to remove characters '\U0000fffc' from nsstring ios

I am developing chat application. In iOS 8, i am sending text and image, while sending image with text a characters "\U0000fffc" are prefixed to nsstring.
I tried using the below code,but it is not working
NSCharacterSet *characterset=[NSCharacterSet characterSetWithCharactersInString:#"\\U0000fffc"];
NSString *newString = [str stringByTrimmingCharactersInSet:characterset];
Any help would be appreciated
Thanks to All who have answered and contributed. I fixed using below code
NSString *codeString = #"\uFFFC";
NSString *msg=[msg stringByReplacingOccurrencesOfString:codeString withString:#""];
If I used above code then compiled errors are fixed and worked....!
NSString *str = #"This is my string \U0000fffc";
NSString *strModified = [str stringByReplacingOccurrencesOfString:#"\U0000fffc" withString:#""];
NSLog(#"%#",strModified);
Try this. Hope will work for you. :)
Today, I have faced the same situation and after digging in some more I have found that the said character is in fact NSAttachmentCharacter which the function -(void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString; adds to your attributed string to mark the position of a NSTextAttachment in your string and you shouldn't replace it anyways.
If you are still facing trouble working with your Strings, provide more information about the problem, otherwise, stick to your current implementation.
This is the replacement character � (often a black diamond with a white question mark or an empty square box) is a symbol found in the Unicode standard at codepoint U+FFFD in the Specials table. It is used to indicate problems when a system is unable to render a stream of data to a correct symbol. It is usually seen when the data is invalid and does not match any character.
This can be removed as follow
NSString *codeString = #"\\ufffc";
NSString *newString=[str stringByReplacingOccurrencesOfString:codeString withString:#""];
P.S : "/uFFFC" didn't worked for me.

Punctuation and return (statements?) in text field? [duplicate]

This question already has answers here:
Is it possible to include a quotation mark as part of an nsstring?
(7 answers)
Closed 9 years ago.
This is really difficult to describe as I don't know what the proper term is here, but I have written a simple self.textfield.text=#"text" line, but **
how can I use punctuation like double-quotes (") without Xcode
thinking its actual code rather than text
** (I think I have an idea but I don't know the syntax).
And **
is it also possible to add returns or enters when setting text like
this?
I am a Java programmer and can do this in that language, but I'm new to app development!
Any Demo/Example will be very much helpful for me.
** Thanks for the help,
The way to do it is to add escape characters:
NSString *string = #"This is a quote: \"To err is human\"";
NSString *string = #"This is a tab:\tTo err is human";
NSString *string = #"This is a newline:\n"To err is human\"";

How can I get a NSString between two strings in Objective-C on iOS? [duplicate]

This question already has answers here:
Remove HTML Tags from an NSString on the iPhone
(22 answers)
Closed 9 years ago.
I have this NSString:
<title>My Friends Website</title>
How do I pull out My Friends Website into it's own NSString on iOS with Objective-C?
try that:
NSString *myString = [[#"<title>My Friends Website</title>" stringByReplacingOccurrencesOfString:#"<title>" withString:#""] stringByReplacingOccurrencesOfString:#"</title>" withString:#""]];

ios when building a string I need to reuse the same placeholder [duplicate]

This question already has answers here:
Is there a way to specify argument position/index in NSString stringWithFormat?
(3 answers)
Closed 9 years ago.
I am making a string that is being used in a SQL statement. I need to reuse the same string within it several times. This string is the text from a textbox.
Current Code:
NSString *searchStr = [NSString stringWithFormat:#"(Name LIKE \'%%%#%%\' OR Contact LIKE \'%%%#%%\')", [self.txtfSearch text], [self.txtfSearch text]];
All the %'s are for a contains search. Please no comments on this method as I am connecting to a webservice that I have no control over currently. Im sure you can all relate.
What I need to do is reuse the [self.txtfSearch text] part at the end. I have looked up the apple dev docs on strings and placeholders. I even tried the C# method of using {0} for the placeholder but I cant get it working.
Doing it as above isnt a big deal if im looking in 2 columns, but there are many cases where its going to be 6 or more and then the string building gets ridiculous.
I want something like this:
NSString *searchStr = [NSString stringWithFormat:#"(Name LIKE \'%%{0}%%\' OR Contact LIKE \'%%{0}%%\')", [self.txtfSearch text]];
Is there a way to do this in obj-c?
Could you do something like this:
NSString *placeholderStr = #"_";
NSString *searchStr = [#"(Name LIKE '%%_%%' OR Contact LIKE '%%_%%'" stringByReplacingOccurrencesOfString: placeholderStr withString: [self.txtfSearch text]];
This will replace all of the underscores (_) with whatever text is in txtfSearch.

Resources