删除特定字符后的空格

时间:2021-03-17 20:14:14

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.

我想在teland之后移除任何空格,最多10个字符。

6 个解决方案

#1


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..!! :)

希望对你有帮助..!! :)

#2


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.

这比用“”替换“”要好一些,因为它使用了charset,你“永远不会知道其他字符集中的空格是多少”。操作系统确实知道,所以更好地信任它。

Since your question isn't 100% clear, I'm assuming that is what you need, but feel free to comment for more help :)

由于您的问题不是100%明确,我认为这就是您所需要的,但随时可以发表评论以获得更多帮助:)

EDIT : I might have misunderstood you. If you need to remove all spaces in the string, you could just use Abha's answer.

编辑:我可能误解了你。如果你需要删除字符串中的所有空格,你可以使用Abha的答案。

EDIT 2 : Okay we're about to solve this outstanding mystery.

编辑2:好的,我们即将解决这个突出的谜团。

You want to trim ALL spaces after telinside your string.

您希望在telinside字符串后修剪所有空格。

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

查找(使用可用的NSString方法)telinside字符串。一旦找到它,你就可以在字符串中找到它的索引(毕竟,字符串只是一个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).

一旦你有索引,你只需要使用Abha的答案(在你找到的索引开始的范围内替换“”和“”的出现,并在那个索引+10(或你需要的任何数字)结束)。

It should be between 2 to 5 lines long, using various NSString methods or, if you really want to, a loop.

它应该在2到5行之间,使用各种NSString方法,或者,如果你真的想要,一个循环。

Answers you should check for inspiration :

Find string in string

在字符串中查找字符串

Replace characters in range

替换范围内的字符

Find index of char in string

在字符串中查找char的索引

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.

虽然,为了交谈,我假设你只需要电话号码(而不是电话)。所以删除所有空格应该足够了(再次,Abha的回答)。当你可能不会使用它时,我没有看到任何理由为什么你会特别注意字符串的第一部分。也许我错了,但你说你是新人,我在想你是以错误的方式接近这个。

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.

另外,要添加其他内容,如果您对服务器有任何控制权,服务器本身不应发送tel:(555)555 5555.这很容易出错。服务器发送一个要显示的字符串,带有正确的字符和漂亮的书写,如电话:(555)555 5555“,或者只接收电话对象中的电话号码(json或其他东西),如5555555555.如果你有对服务器的任何控制,使它发送正确的信息,而不是发送不实用的东西,并不得不再次返工。

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.

请注意,通常,这是第二种选择。服务器发送原始内容,您只需将其修改为必要时看起来很好。不是相反。

#3


0  

You need to use NSMutableString class and its manipulation functions provided itself.

您需要使用NSMutableString类及其自身提供的操作函数。

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:

如果你的字符串是动态的,你真的不知道你需要删除多余空格的索引,请使用NSString类的下面方法:

stringByReplacingOccurrencesOfString: Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.

stringByReplacingOccurrencesOfString:返回一个新字符串,其中接收器中所有出现的目标字符串都被另一个给定字符串替换。

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类,请参阅下面的详细信息以及上面的示例:

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.

NSMutableString:NSMutableString类声明了一个对象的编程接口,该对象管理一个可变字符串 - 即一个可以编辑其内容的字符串 - 从概念上表示一个Unicode字符数组。要构造和管理不可变的字符串或在创建后无法更改的字符串,请使用NSString类的对象。

#4


0  

So you want to remove space from first 10 character, which you think is a tel number, right?

所以你想从前10个字符中删除空格,你认为这是一个电话号码,对吧?

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.

这是你想要做的吗?我已经逐步写下来以便更好地理解。

#5


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.

并且您将获得您想要的空间的服务器字符串。同样,这可能仅适用于您当前的解决方案。

#6


-1  

NSString *string = @"(555) 555-5555" //here you need to assign string which you are getting from server

NSString * string = @“(555)555-5555”//这里你需要指定从服务器获取的字符串

NSString *resultString = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];

NSString * resultString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

resultString will be the string with no space at start and end of the string.

resultString将是字符串开头和结尾没有空格的字符串。

Hope this helps!

希望这可以帮助!

#1


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..!! :)

希望对你有帮助..!! :)

#2


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.

这比用“”替换“”要好一些,因为它使用了charset,你“永远不会知道其他字符集中的空格是多少”。操作系统确实知道,所以更好地信任它。

Since your question isn't 100% clear, I'm assuming that is what you need, but feel free to comment for more help :)

由于您的问题不是100%明确,我认为这就是您所需要的,但随时可以发表评论以获得更多帮助:)

EDIT : I might have misunderstood you. If you need to remove all spaces in the string, you could just use Abha's answer.

编辑:我可能误解了你。如果你需要删除字符串中的所有空格,你可以使用Abha的答案。

EDIT 2 : Okay we're about to solve this outstanding mystery.

编辑2:好的,我们即将解决这个突出的谜团。

You want to trim ALL spaces after telinside your string.

您希望在telinside字符串后修剪所有空格。

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

查找(使用可用的NSString方法)telinside字符串。一旦找到它,你就可以在字符串中找到它的索引(毕竟,字符串只是一个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).

一旦你有索引,你只需要使用Abha的答案(在你找到的索引开始的范围内替换“”和“”的出现,并在那个索引+10(或你需要的任何数字)结束)。

It should be between 2 to 5 lines long, using various NSString methods or, if you really want to, a loop.

它应该在2到5行之间,使用各种NSString方法,或者,如果你真的想要,一个循环。

Answers you should check for inspiration :

Find string in string

在字符串中查找字符串

Replace characters in range

替换范围内的字符

Find index of char in string

在字符串中查找char的索引

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.

虽然,为了交谈,我假设你只需要电话号码(而不是电话)。所以删除所有空格应该足够了(再次,Abha的回答)。当你可能不会使用它时,我没有看到任何理由为什么你会特别注意字符串的第一部分。也许我错了,但你说你是新人,我在想你是以错误的方式接近这个。

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.

另外,要添加其他内容,如果您对服务器有任何控制权,服务器本身不应发送tel:(555)555 5555.这很容易出错。服务器发送一个要显示的字符串,带有正确的字符和漂亮的书写,如电话:(555)555 5555“,或者只接收电话对象中的电话号码(json或其他东西),如5555555555.如果你有对服务器的任何控制,使它发送正确的信息,而不是发送不实用的东西,并不得不再次返工。

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.

请注意,通常,这是第二种选择。服务器发送原始内容,您只需将其修改为必要时看起来很好。不是相反。

#3


0  

You need to use NSMutableString class and its manipulation functions provided itself.

您需要使用NSMutableString类及其自身提供的操作函数。

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:

如果你的字符串是动态的,你真的不知道你需要删除多余空格的索引,请使用NSString类的下面方法:

stringByReplacingOccurrencesOfString: Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.

stringByReplacingOccurrencesOfString:返回一个新字符串,其中接收器中所有出现的目标字符串都被另一个给定字符串替换。

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类,请参阅下面的详细信息以及上面的示例:

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.

NSMutableString:NSMutableString类声明了一个对象的编程接口,该对象管理一个可变字符串 - 即一个可以编辑其内容的字符串 - 从概念上表示一个Unicode字符数组。要构造和管理不可变的字符串或在创建后无法更改的字符串,请使用NSString类的对象。

#4


0  

So you want to remove space from first 10 character, which you think is a tel number, right?

所以你想从前10个字符中删除空格,你认为这是一个电话号码,对吧?

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.

这是你想要做的吗?我已经逐步写下来以便更好地理解。

#5


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.

并且您将获得您想要的空间的服务器字符串。同样,这可能仅适用于您当前的解决方案。

#6


-1  

NSString *string = @"(555) 555-5555" //here you need to assign string which you are getting from server

NSString * string = @“(555)555-5555”//这里你需要指定从服务器获取的字符串

NSString *resultString = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];

NSString * resultString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

resultString will be the string with no space at start and end of the string.

resultString将是字符串开头和结尾没有空格的字符串。

Hope this helps!

希望这可以帮助!