How to remove WhiteSpace between base64 string in objective c? - ios

I referred to remove whitespaces from base64 encoded string when posting but still I have this problem
I am currently using the following code :
NSString *TrimBase64String=[base64String stringByReplacingOccurrencesOfString:#" " withString:#""];

Easy Peasy, use this line:
NSString *TrimBase64String=[base64String stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

Use below lines of code to create NSString with Base64 Encoding to avoid white spaces and then post it:
NSString *strbase64 = [YourNSData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
Check answer here, may be it will help more:
encode image to base64, get a invalid base64 string (ios using base64EncodedStringWithOptions)

I tried many ways and finally found the cause of the problem is the character \r
\r = CR (Carriage Return) // Used as a new line character in Mac OS before X`
[yourBase64String stringByReplacingOccurrencesOfString:#"\r" withString:#""];

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!

iOS Unicode Encoding

I am getting this kind of data ElbowWristHand_DeQuervian\U00e2\U0080\U0099s Tenosynovitis through a web service, But actually the content is "ElbowWristHand_DeQuervian's". I have followed all the methods mentioned in the following link:
Using Objective C/Cocoa to unescape unicode characters, ie \u1234
but still i am unable to convert the unicode characters to the proper string. Please suggest.
Thanks
While initializing string us unicode string encoding -
NSString *dataString = [[NSString alloc] initWithData:recievedData usingEncoding:NSUnicodeStringEncoding]

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.

Parse RTF file into NSArray - objective C

I'm trying to parse and English Dictionary from an RTF file into an array.
I originally had it working, but I'm not sure why it isn't now.
In the dictionary, each word is separated by a new line (\n). My code so far is:
//loading the dictionary into the file and pull the content from the file into memory
NSData *data = [NSData dataWithContentsOfFile:#"wordList.rtf"];
//convert the bytes from the file into a string
NSString *string = [[NSString alloc] initWithBytes:[data bytes]
length:[data length]
encoding:NSUTF8StringEncoding];
//split the string around newline characters to create an array
NSArray *englishDictionary = [string componentsSeparatedByString:#"\n"];
When I try and NSLog it, it just comes up with (null)...
I've already looked at: Objective-C: Reading a file line by line
But couldn't get the answer by Yoon Lee working properly. It prints out with two back slashes at the end as well as lots of unnecessary stuff at the start!
Any help would be greatly appreciated! Cheers!
Try using a plain text (txt) file instead of rtf. RTF files contain formatting information about the text as well. Thats the unnecessary stuff that you see after reading the content.

Facebook iOS sdk search encoding

Please help with search string encoding. Simple request like this:
https://graph.facebook.com/search?q=max&type=user
works pretty good, bud when I try to search in cyrillic:
https://graph.facebook.com/search?q=макс&type=user
I've got null result. What encoding should I use?
Here is right encodings; First time i forgot replace spaces with pluses.
searchString = [searchString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
searchString = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Resources