I have an string which is coming from a server :
<a href=\"tel:(555) 555-5555\">(555) 555-5555</a>
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..!! :)Answer2:
Removing spaces is called Trimming. You can find a possible solution <a href="https://stackoverflow.com/a/5756341/3603502" rel="nofollow">here</a>, or <a href="https://stackoverflow.com/a/4645698/3603502" rel="nofollow">here</a>
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 :
<a href="https://stackoverflow.com/a/2754075/3603502" rel="nofollow">Find string in string</a>
<a href="https://stackoverflow.com/a/8953089/3603502" rel="nofollow">Replace characters in range</a>
<a href="https://stackoverflow.com/questions/12385331/find-the-index-of-a-character-in-a-string" rel="nofollow">Find index of char in string</a>
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.Answer3:
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 :<ul><li>substringFromIndex:</li> <li>substringWithRange:</li> <li>substringToIndex:</li> </ul>
If your string is dynamic and you really dont know the index from where you need to remove extra spaces use below method of
<strong>stringByReplacingOccurrencesOfString:</strong> Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.
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 <strong>see detail below</strong> and example above on the top:
<strong>NSMutableString:</strong> <em>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.</em>Answer4:
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.Answer5:
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.Answer6:
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!