0

我刚刚更新到 ios 7 sdk,我想修剪/替换字符串字符之间的空格,从而从 ABAddressBook 中取出数字。

我尝试使用下面的替换“”与“”代码,但此代码似乎在 ios7 sdk 中不起作用,顺便说一句,它在 ios 6 sdk 中运行良好。

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

有没有其他方法可以在 IOS 7 中做到这一点?

编辑:

这是我正在尝试的电话号码类型。

输入:"+65 12 345 6789"

我从 NSLog 得到的输出是" 12 345 6789"

我意识到,当我添加到 NSDictionary 并在 NSLog 中查看它时,它似乎包含 \u00a0 的 unix 代码表示,类似于不等于句号的“中间点”。

提前致谢。

4

4 回答 4

1

从这里找到答案

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

// 在哪里 @”。” 是通过键入Option + Spacebar创建的

该号码是从 ABAddressbook 中提取的。

于 2014-01-22T07:05:03.693 回答
0

只要有任何空格,您就可以遍历字符串并删除空格

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

上面字符串的结果是Astringwithmultiplespacesandotherwhitespace.

于 2013-10-03T07:35:34.940 回答
-1

尝试这个:

NSString *str = @"   untrimmed   string   ";
NSString *trimmed = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
于 2013-10-03T07:25:01.253 回答
-2

尝试这个

[yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

whitespaceCharacterSet Apple Documentation for iOS 说

返回以二进制格式编码接收器的 NSData 对象。

  • (NSData *)bitmapRepresentation 返回值 以二进制格式编码接收器的 NSData 对象。

讨论 此格式适用于保存到文件或以其他方式传输或存档。

字符集的原始位图表示是 2^16 位(即 8192 字节)的字节数组。位置 n 的位值表示字符集中存在十进制 Unicode 值 n 的字符。要测试原始位图表示中是否存在具有十进制 Unicode 值 n 的字符,请使用如下表达式:

所以试试这个

NSString *testString = @"  Eek! There are leading and trailing spaces  ";
NSString *trimmedString = [testString stringByTrimmingCharactersInSet:
                             [NSCharacterSet whitespaceAndNewlineCharacterSet]];
于 2013-10-03T07:23:16.817 回答