简体   繁体   English

修剪字符之间的空格

[英]Trim whitespace in between characters

i just updated to ios 7 sdk, and I would like to trim/replace the whitespace between characters of a string whereby the numbers are taken out from ABAddressBook. 我刚刚更新为ios 7 sdk,我想修剪/替换字符串字符之间的空格,从而从ABAddressBook中取出数字。

I have tried using the replace " " with "" code below, but this code doesnt seems to work in ios7 sdk, it works fine in ios 6 sdk by the way. 我曾尝试使用下面的代码替换“”,但此代码在ios7 sdk中似乎不起作用,顺便说一下,它在ios 6 sdk中工作正常。

NSString *TrimmedNumberField = [self.numberField.text 
stringByReplacingOccurrencesOfString:@" " withString:@""];

is there any other way I could do it in IOS 7? 在IOS 7中还有其他方法可以做到吗?

EDIT: 编辑:

It's a phone number type that I'm trying. 我正在尝试的电话号码类型。

Input: "+65 12 345 6789" 输入: "+65 12 345 6789"

The output i got from NSLog is " 12 345 6789" 我从NSLog获得的输出是" 12 345 6789"

I realized that when I added into NSDictionary and view it in NSLog, it appears that it contains a unix code representation of \  which is similar to the "dot in the middle" which is not equals to a fullstop. 我意识到,当我添加到NSDictionary中并在NSLog中查看它时,它似乎包含\\ u00a0的unix代码表示,类似于“中间的点”,不等于句号。

thanks in advance. 提前致谢。

Found the answer from here 这里找到答案

phoneNumber = [phoneNumber stringByReplacingOccurencesOfString:@"." withString:@""];

// where @"." //其中@“​​。 was created by typing Option + Spacebar 是通过键入Option + 空格键创建的

The number is extracted from ABAddressbook. 该号码是从ABAddressbook中提取的。

You can loop over the string and remove whitespace as long as there is any 您可以遍历字符串并删除空格,只要有

NSString *someString = @"A string with   multiple spaces and    other whitespace.";

NSMutableString *mutableCopy = [someString mutableCopy];

// get first occurance of whitespace
NSRange range = [mutableCopy rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];

// If there is a match for the whitespace ...
while (range.location != NSNotFound) {
    // ... delete it
    [mutableCopy deleteCharactersInRange:range];
    // and get the next whitespace
    range = [mutableCopy rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
}

// no more whitespace. You can get back to an immutable string
someString = [mutableCopy copy];

The result with the string above is Astringwithmultiplespacesandotherwhitespace. 上面的字符串的结果是Astringwithmultiplespacesandotherwhitespace.

Try This: 尝试这个:

NSString *str = @"   untrimmed   string   ";
NSString *trimmed = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

Try This 尝试这个

[yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

whitespaceCharacterSet Apple Documentation for iOS says whitespaceCharacterSet iOS的Apple文档说

Returns an NSData object encoding the receiver in binary format. 返回以二进制格式编码接收器的NSData对象。

  • (NSData *)bitmapRepresentation Return Value An NSData object encoding the receiver in binary format. (NSData *)bitmapRepresentation返回值一个以二进制格式对接收器进行编码的NSData对象。

Discussion This format is suitable for saving to a file or otherwise transmitting or archiving. 讨论此格式适合于保存到文件或以其他方式传输或存档。

A raw bitmap representation of a character set is a byte array of 2^16 bits (that is, 8192 bytes). 字符集的原始位图表示形式是2 ^ 16位(即8192字节)的字节数组。 The value of the bit at position n represents the presence in the character set of the character with decimal Unicode value n. 位置n的位的值表示十进制Unicode值n的字符存在于字符集中。 To test for the presence of a character with decimal Unicode value n in a raw bitmap representation, use an expression such as the following: 要测试原始位图表示中是否存在带十进制Unicode值n的字符,请使用以下表达式:

So Try This 所以试试这个

NSString *testString = @"  Eek! There are leading and trailing spaces  ";
NSString *trimmedString = [testString stringByTrimmingCharactersInSet:
                             [NSCharacterSet whitespaceAndNewlineCharacterSet]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM