开发者

Replace two or more spaces to &nbsp,&nbsp

How can I enc开发者_运维问答ode convert spaces to &nbsp in objective-c?

I need each space is replaced by &nbsp, except when only one space

Example

in  text: AAA    BBB C
out text: AAAnbsp;nbsp;nbsp;nbsp;BBB C

thanks


Assuming you're talking about NSStrings -- look at the NSString documentation. There are instance methods for replacing strings of the form stringByReplacing...


Given that you use NSString or NSMutableString:

myString = [myString stringByReplacingOccurrencesOfString:@"  " withString:@"  "];
myString = [myString stringByReplacingOccurrencesOfString:@"  " withString:@"  "];

Yes, this needs to be done twice for the case of an odd number of spaces in a row.


If you are dealing with C-strings instead of NSStrings or NSMutableStrings, you do the same but with a function instead of a method. I am not going to write such a function here but know that you have to run it twice. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜