0

I have an string which is coming from a server :

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

I want to remove any space after teland up to 10 characters.

Gil Sand
  • 5,333
  • 3
  • 29
  • 66
AADi
  • 19
  • 4

6 Answers6

1

For removing only spaces between characters you can use this

NSString *strNum = @"(555) 555-5555";
strNum = [strNum stringByReplacingOccurrencesOfString:@" " withString:@""];

Hope this will help you..!! :)

Abha
  • 957
  • 1
  • 12
  • 36
  • yes I did but didn't work because that is dynamic string on the place of that (555) 555-5555 anything can come so from this

    (555) 555-5555 

    I have o remove if there is any space after tel to 10 characters.
    – AADi Sep 22 '16 at 07:19
  • Than first of all try to do which Zil suggested and after that using mine's method i.e First of all take your dynamic string in NSString and do trimming after that use stringByReplacingOccurrencesOfString. – Abha Sep 22 '16 at 07:21
  • stringByReplacingOccurrencesOfString will trim all the spaces, but I want to trim only, If there is any space after tel: "tel:(555) 555-5555" in this – AADi Sep 22 '16 at 07:23
  • I think he means after the "TEL:" string inside the given string. Not the actual telephone number. – Gil Sand Sep 22 '16 at 07:25
  • 1
    Wait a moment..What you need ? Removing space between phone number and tel: ?? – Abha Sep 22 '16 at 07:25
  • Are you trying Trimming ? – Abha Sep 22 '16 at 07:26
  • yes @Zil is right i want to trim after a special index from wherever in string if there is tel then,for only 10 characters after that should be trim – AADi Sep 22 '16 at 07:28
  • What happend when you are using trimming ? – Abha Sep 22 '16 at 07:29
  • it is trimming all the spaces – AADi Sep 22 '16 at 07:31
1

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.

Community
  • 1
  • 1
Gil Sand
  • 5,333
  • 3
  • 29
  • 66
  • (555) 555-5555  I have o remove if there is any space after tel to 10 characters – AADi Sep 22 '16 at 07:14
  • Copying what is unclear in the question's comment in the answer isn't gonna clarify. My solution removes all spaces at the beginning AND the end. – Gil Sand Sep 22 '16 at 07:15
  • @Zil trimming whitespace only removing extra spaces from starting point and end point, but he needs to be remove space between character – Abha Sep 22 '16 at 07:17
  • Are you sure that's what he needs? He says "after tel", which I assume means after the phone numbers. – Gil Sand Sep 22 '16 at 07:18
  • that is dynamic string on the place of that (555) 555-5555 anything can come so from this

    (555) 555-5555 

    I have o remove if there is any space after tel to 10 characters.
    – AADi Sep 22 '16 at 07:19
  • We understand that. but what do you mean by "any space after tel to 10 characters" ? Do you have to removes all spaces after the phone number? Or ALL the spaces inside the phone number? Or all characters so you have exactly 10 characters at the end? – Gil Sand Sep 22 '16 at 07:22
  • @AADi you are misinterpret us. First of all please try all suggested answer – Abha Sep 22 '16 at 07:22
  • @Abha I did try but it didn't work... bcz stringByReplacingOccurrencesOfString will trim all the spaces, but I want to trim only, If there is any space after tel: "tel:(555) 555-5555" in this – AADi Sep 22 '16 at 07:25
  • I edited my answer with the thought process of what you need to do. – Gil Sand Sep 22 '16 at 07:29
  • I added links to pretty much everything you need to make the answer yourself. – Gil Sand Sep 22 '16 at 07:33
  • 1
    i think we have given enough explanation regarding aadi's prob now he needs to apply himself .. +1 for your explanation. – vaibhav Sep 22 '16 at 07:45
0

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.

vaibhav
  • 3,893
  • 1
  • 19
  • 46
  • that is dynamic string on the place of that (555) 555-5555 anything can come so from this

    (555) 555-5555 

    I have o remove if there is any space after tel to 10 characters.
    – AADi Sep 22 '16 at 07:20
0

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.

shuvo
  • 529
  • 4
  • 15
0

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.

-1

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!

Satish Mavani
  • 4,439
  • 2
  • 16
  • 27