Remove Space After a Particular Character


I have an string which is coming from a server :

<a href=\"tel:(555) 555-5555\">(555) 555-5555</a>&nbsp;

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 <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.


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 NSString class:

<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>


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!


  • Fast way to alphabetically sort the contents of a file in java
  • Is there an easy way to associate an event with a ListViewItem?
  • Creating PDF from TIFF image using iText
  • Why must we declare a variable name when adding a method to a struct in Golang?
  • For loop with if condition on multiple R functions
  • Row to Column conversion in Talend
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • ThreadStatic in asynchronous ASP.NET Web API
  • Sending keystrokes/mouse clicks to a Java program with Autohotkey
  • Custom preprocessing in caret
  • calculate gradient output for Theta update rule
  • Is it possible to open regedit and navigate to straight to a specific key using process.start?
  • Hardware Accelerated Image Scaling in windows using C++
  • Refering to the class itself from within a class mehod in Objective C
  • How to do unit test for HttpContext.Current.Server.MapPath
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Azure Cloud Service Web Role web pages do not load
  • swift auto completion not working in Xcode6-Beta
  • How to add date and time under each post in guestbook in google app engine
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Convert array of 8 bytes to signed long in C++
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Cannot Parse HTML Data Using Android / JSOUP
  • Linker errors when using intrinsic function via function pointer
  • Proper way to use connect-multiparty with express.js?
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • C# - Getting references of reference
  • Angular 2 constructor injection vs direct access
  • coudnt use logback because of log4j
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal