Remove Space After a Particular Character - ios

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!

Related

split string in ios string from json data?

I have one string. Now I wanted to split this string. For static separation I know the code but I don’t code for dynamic value.
my string is
NSString *str = #"https://graph.facebook.com/v2.5/181054825200000/feed?fields=created_time,message,picture,full_picture,comments.limit%280%29.summary%28true%29,likes.limit%280%29.summary%28true%29&limit=5&format=json&access_token=CAALjFrE5mNYBAOg1EDiUrsE2kr1kIRrLIv7g4OweSMvHso2exB5Dttshn7dgOlW24ZCXSnDZAWiV6xMUKXedTXUhiHpdmZBPCGzD1orFlrLRP2gaBZCbZBZBnjUHewF9hZBmJKxtiwVzpw9gnnQXk5Hfx0ZBM2ksAUzkSWR5feaNMbf3UUmUpJlxeh0gKdDrzWBvIJRPy0xGqL0ZAMFsRhyCZCTX42l1sZAceZB0VCeDZB95mrAZDZD&until=1456345291&__paging_token=enc_AdCKD3tSYMoZB3MCKaJkYnbVmBgUyY2tBceGDD2G1hqxRDiQKZCsSbmvWZASLvlCMf0BVzq2uZAScSWp7ZAavZB2d72BIHJISefk09noRuv9gA5b5hFwZDZD";
but i don’t how to show any value dynamically .(for e.g. until (in string))
please help me for this issue.
Thank You.
If you are parsing a URL you should really use NSURLComponents. It makes breaking a URL into the different parts much easier, and the code is tested and verified by Apple.
For separate string by a separator you can use this.
NSString *url = #"<url>";
NSArray *array = [url componentsSeparatedByString:#"<seperator string>"];
NSLog(#"%#", array);
But for URL parsing ,As per Duncan's answer, yes it is good to parse a URL using NSURLComponents. By using this class you can get any desired part of an URL.

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.

cant get 100,000+ hebrew characters into objective-c string

I have 100,000+ characters of text that need to be converted into a string so I can count the characters and display them on a page correctly, but in the text there are tons of quotations ("") and lots of commas, so it doesnt even turn into a string.
Does anyone know a way that you can ignore quotations and commas inside a NSString without having to do this \"" each time?
Here's some of the text. its english/hebrew
Psalm 30
...
Psalm 100
...
The following Psalm is not to be said on Shabbat, Festivals, the day before Pesach, Chol HaMoed Pesach, and the day of Yom Kippur
...
You say “I cant even turn the text into a string”. Since you said (in a comment) you're “just pulling it off this website”, the simplest way to do this is +[NSString stringWithContentsOfURL:encoding:error:]. This works for me:
NSURL *url = [NSURL URLWithString:#"http://opensiddur.org/wp-content/uploads/2010/08/The-Blessing-Book-Nusa%E1%B8%A5-Ha-Ari-%E1%B8%A4aBaD-3.2.txt"];
NSError *error;
NSString *text = [NSString stringWithContentsOfURL:url usedEncoding:nil error:&error];
NSLog(#"error=%# text.length=%lu", error, (unsigned long)text.length);
You can look into NSURLSession or NSURLConnection when you want to do it in a non-blocking fashion.
If you plan to distribute the text in a file (named, let's say, “blessingBook.txt”) in your app bundle, you can get the URL this way:
NSURL *url = [[NSBundle mainBundle] URLForResource:#"blessingBook" withExtension:#"txt"];
If you're loading it directly from your app bundle, you probably don't need to worry about using NSURLSession to load it in the background. You might want to do your “processing” in the background though, if it takes a while.
You can replace the punctuation or commas or what ever you want to #"" (empty string).
yourString=[yourString stringByReplacingOccurrencesOfString:#"," withString:#""];
yourString=[yourString stringByReplacingOccurrencesOfString:#":" withString:#""];
yourString=[yourString stringByReplacingOccurrencesOfString:#";" withString:#""];
What ever the text you want to replace. Replace as above.
But it is lengthy process finding un wanted quotations, commas..some characters and replace with empty string..
Hope it helps you..!

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.

Cut one string into two others in Objective-C

I have an NSURLRequest being made that to a server that returns a string.
string = [[NSMutableString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
receivedData is the mutable array that the downloaded data is stored in. Everything works fine.
I have now, however, added another value to that string. An example of the returned string would be 14587728000000 , 376.99. Originally it was one value so I didn't have to do any splicing. But, now that I have another value, I want to be able to separate it into two different strings.
What should I do to separate the two values into different string? Some kind of search that goes till the first space, or something like that. I have access to the server, and the string is generated in PHP so the separator can be anything.
You can do this with the NSString componentsSeparatedByString method:
NSString *string = #"14587728000000,376.99";
NSArray *chunks = [string componentsSeparatedByString: #","];
You can find some other common NSString tricks (where I found this one) here.
Use -(NSArray *)componentsSeparatedByString: and pass in the token to split by.

Resources